1

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.

1 Answer 1

1
$sm->get('ZfcTwigRenderer');

Use this code to access twig renderer

example :

public function onBootstrap(MvcEvent $e)
    {

        $ZfcTwigRenderer = $e->getApplication()->getServiceManager()->get('ZfcTwigRenderer');


    }
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the answer. I'm a little lost, should I place this in my Module.php on method onBootstrap()? how can I get the service manager there? Sorry I'm new to ZF. Thank you very much.
Thank you but it still doesn't work. Is $ZfcTwigRenderer->navigation = $navigation; enough or I need to call a method to set it?
where do you use $ZfcTwigRenderer->navigation in your templates? in layout or in template? syntax going to be different
I'm using it inside onBootstrap(). After $ZfcTwigRenderer = $e->getApplication()->getServiceManager()->get('ZfcTwigRenderer'); I call $ZfcTwigRenderer->navigation = $navigation.
No changes in my layout.twig code above. I just changed the contents of my onBootstrap() method in my Module.php to reflect your answer but I still have the same problem. I really appreciate your reply. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.