Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8228209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:23:15+00:00 2026-06-07T16:23:15+00:00

I have a twig-template (with twitter bootstrap) with the following code – it’s a

  • 0

I have a twig-template (with twitter bootstrap) with the following code – it’s a navbar:

<ul class="nav">
    <li class="active"><a href="#">Modul 1</a></li>
    <li><a href="#about">Modul 2</a></li>
    <li><a href="#contact">Modul 3</a></li>
</ul>

This code is in my global.html.twig. So I want to change the class=”active” in the list element depending on the page that is opened.

How can I achieve this without adding too much logic in my template?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:23:18+00:00Added an answer on June 7, 2026 at 4:23 pm

    The only way to achieve this without too much overhead is the use of KnpLabs/KnpMenuBundle.

    In your global.html.twig:

    <ul class="nav">
      {{ knp_menu_render('YourModule1Bundle:MenuBuilder:module1Menu', {'currentClass': 'active'}) }}
      {{ knp_menu_render('YourModule2Bundle:MenuBuilder:module2Menu', {'currentClass': 'active'}) }}
      {{ knp_menu_render('YourModule3Bundle:MenuBuilder:module3Menu', {'currentClass': 'active'}) }}
    </ul>
    

    In your Module1 bundle directory:

    • Create a Menu folder
    • Create MenuBuilder.php as follows:

    –

    <?php
    namespace YourMenu1Bundle\Menu;
    
    use Knp\Menu\FactoryInterface;
    use Symfony\Component\DependencyInjection\ContainerAware;
    
    class MenuBuilder extends ContainerAware
    {
      public function applicationMenu(FactoryInterface $factory, array $options)
      {
          $rootMenu = $factory->createItem('rootMenu');
          $rootMenu->setChildrenAttributes(array('class' => 'nav'));
          $rootMenu->setCurrentUri($this->container->get('request')->getRequestUri());
          $menu = $rootMenu->addChild('Module 1', array('route' => 'the_wanted_route'));
          return $rootMenu;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a twig variable html . To show it in a twig template
I have a base Twig template that has a search bar form in it
I have a view template that is currently FooBundle:Bar:baz.html.twig . However, since I have
I'm developing a simple page with Symfony2, using Twig as template engine. I have
I have two template files. One is a twig template and another is a
For rendering form errors in a twig template, you just have to use the
I have a Symfony2 Twig template. I want to output the value of a
I have the following (Symfony 2) PHP Template: <?php $messages = $view['session']->getFlashes(); if (count($messages)
I'm in a Twig template, and I have a form variable that represents a
I am having some trouble telling twig which template to load. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.