0

I use PHP Phalcon and I'd like to implement two different menus. First for regular user and second for admin. I want to have two menu templates to expand menus easily.

Now I have something like this:

//Main template:

[...]

if (session->role == 'admin')
    include 'adminmenu.volt'
elseif (session->role == 'user')
    include 'usermenu.volt'

 {{ get_content() }} //from upper templates

[...]

(Unlogged user doesn't have menu.)

I think this is not entirely correct. First of all i have no idea how to highlight active position. Secondly I'd like to control menu from controller, for example add special option by change something in database. Is there a proper way to do this? I'd like to get simple solution without using javascript, modules or microservices

Robin71
  • 383
  • 5
  • 26

1 Answers1

0

You're probably looking for partials. In your base template, you're going to want something like:

{% if this.session->get('role') == 'admin' %}
    {{ partial('adminmenu') }}
{% else %}
    {{ partial('usermenu') }}
{% endif %}

You'll have to set your partials directory up on your view:

//This is a path relative to your views directory.
//The following is accurate if your partials directory is a child of your views directory.
$this->view->setPartialsDir('partials');

Once you've done that, executing {{ partial('name') }} will reference a name.volt file in your partials directory.

As for highlighting your position. Your partial can call the dispatcher and ask what the controller and action are:

{{ this.dispatcher.getControllerName() }} and {{ this.dispatcher.getControllerName() }}

You can check the value of those against your href attribute and set the active flag if it is true.

user1119648
  • 531
  • 1
  • 5
  • 16