Good day,
I have a twig layout template with contents of the ZF2 skeleton (zfctwig version) and I want to have a dynamic main navigation using a variable that I will set in module.config.php.
layout.twig:
<div class="nav-collapse collapse">
<ul class="nav">
{% for item in navigation %}
<li {% if item.active %} class="active" {% endif %}>
<a href="{{ url(item.route) }}">
{{ translate(item.name) }}
</a>
</li>
{% endfor %}
</ul>
</div><!--/.nav-collapse -->
I will pass it using the onBootstrap() method in my Module.php:
Module.php:
$navigation = array(
'home' => array(
'name' => 'Home',
'route' => 'home',
),
'profile' => array(
'name' => 'Profile',
'route' => 'myroute',
'active' => true
),
);
$view = $e->getApplication('application')->getMvcEvent()->getViewModel();
$view->navigation = $navigation;
The $navigation variable will be placed in my module.config.php after I got this working.
This code works when I'm not using twig in my template (e.g. layout.phtml with no twig codes) but when I am using the above layout.twig, {{navigation}} is empty.
I think that I should not be using
$view = $e->getApplication('application')->getMvcEvent()->getViewModel();
since I think it returns the default zf2 ViewModel and not the twig version but I don't know how it's retrieved.
If this is not the problem, what am I doing wrong? and if I got it working, how can I change the active index of $navigation['profile'] to false in one of my controllers?
Thank you very much!
UPDATE:
It seems that nobody had experience the same problem. Am I doing this the wrong way? Is there a better way to do this? The goal is to create a dynamic main menu and navigation that can be modified using the settings inside a module.config.php rather than updating the view file constantly. Changing it in a specific controller is not good either since I want it to be global since the main menu will be the same on every page. I searched the web but I can't find a way to do it using twig.