Dynamic menu

### Navigation

- index
- next |
- previous |
- fantastico 0.4.0-b114 documentation »
- Fantastico features »
- Built in components »

# Dynamic menu

Menus are a core part of every web site / application as well as mobile applications. More over, again and again developers will want a quick way to define menu items without actually redefining menu data structure again and again. This component which we generic named dynamic menu simply provides the controller and the model for easy development of menus.

## Integration

In order to use dynamic menu component within your project follow the steps below:

### Component files activation deprecated

1. Create a symbolic link under your root components folder to dynamic_menu.

       mkdir <components root>/dynamic_menu
       cd <components root>/dynamic_menu
       ln -s ../../pip-deps/lib/python[version]/site-packages/fantastico/contrib/dynamic_menu/sql .
       ln -s ../../pip-deps/lib/python[version]/site-packages/fantastico/contrib/dynamic_menu/tests .
       ln -s ../../pip-deps/lib/python[version]/site-packages/fantastico/contrib/dynamic_menu/*.py .

### Component files activation (SDK)

    fsdk activate-extension --name dynamic_menu --comp-root <comp root>

### Component sample + db data

1. Create a template in one of your components in which you define the menu view:

       <!-- *sample_menu.html* - simple snippet for creating a left / right side dockable menu. -->
       {% for menu_item in model["items"] %}
          <a href="{{menu_item.url}}" title="{{menu_item.title}}" target="{{menu_item.target}}">{{menu_item.label}}</a><br/>
       {% endfor %}

2. In all views where you want to reuse the component you can paste the following snippet:

       {% component template="sample_menu.html", url="/dynamic-menu/menus/1/items/" %}{% endcomponent %}

3. Make sure you run dynamic_menu/sql/module_setup.sql against your configured database.

4. This script will create menus and menu_items tables into your database. Below you can find a sample script for creating a menu:

       INSERT INTO menus(name) VALUES('My First Menu');
       INSERT INTO menu_items(target, url, title, label)
       VALUES ('_blank', '/homepage', 'Simple and friendly description', 'Home', <menu_id from previous step>),
              ('_blank', '/page2', 'Simple and friendly description', 'Page 2', <menu_id from previous step>),
              ('_blank', '/page3', 'Simple and friendly description', 'Page 3', <menu_id from previous step>);

   By default, when this component is first setup into an application, the sample menu mentioned above is created in database. You can test to see that dynamic menu works by accessing dev server url: http://localhost:12000/dynamic-menu/menus/1/items/.

## Current limitations

Because Fantastico framework is developed using an Agile mindset, only the minimum valuable scope was delivered for Dynamic Menu component. This mean is not currently possible to:

> - Localize your menu items.
> - Display the menu items in the request language dynamically.
> - Only first 100 menu items can be currently retrieved.

## Technical summary

class `fantastico.contrib.dynamic_menu.menu_controller.` `DynamicMenuController`(settings_facade)

  This class provides the controller for dynamic menus. The following routes are automatically made available when dynamic menu component is deployed:

  > /dynamic-menu/menus/<menu_id>/items/ – This route loads menu items from database and retrieve them in json format.

  Below you can see a diagram describing relation model of the menu:

  `max_items`

    This property retrieves the maximum number of items allowed for a menu.

  `retrieve_menu_items`(*args, **kwargs)

    This method is used to retrieve all items associated with a specified menu.

    Parameters: | request (HTTP request) – Http request being processed. menu_id (int) – Menu unique identifier we want to retrieve information for.
    Returns: | A JSON array containing all available menu items.
    Raises fantastico.contrib.dynamic_menu.menu_exceptions.FantasticoMenuNotFoundException:
    | Whenever the requested menu does not exist.

class `fantastico.contrib.dynamic_menu.menu_exceptions.` `FantasticoMenuNotFoundException`

  This class defines a concrete fantastico menu not found exception raised whenever someone tries to access an inexistent menu attributes.