0
votes

I use the Symfony2 framework combined with the FOSUserBundle. I modified the layout by overwriting some FOSUserBundle templates. This is working very well! The only problem is that I cannot set variables in my templates that extend templates from the FOSUserBundle that are overwritten by my own templates. Difficult to describe, but I try it:

I am overriding the Registration/register_content.html.twig like this:

{% trans_default_domain 'FOSUserBundle' %}
{% set section='register' %}

<h1>{{ 'Anmeldung'|trans }}</h1>
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST">
    {{ form_widget(form) }}
    <div>
        <input type="submit" value="{{ 'registration.submit'|trans }}" />
    </div>
</form>

(/home/simon/server/html/portal/app/Resources/FOSUserBundle/views/Registration/register_content.html.twig)

The point is that I want to set the section variable for my bundle's layout.html.twig to highlight the current section in the menu. This works fine with my regular templates (that are not overwritten from FOSUserBundle)

I also have overwritten layout.html.twig from FOSUserBundle:

{% extends 'MyBundle::layout.html.twig' %}

{% block content %}
    {% block fos_user_content %}{% endblock %}
{% endblock %}

(/home/simon/server/html/portal/app/Resources/FOSUserBundle/views/layout.html.twig)

If I set my variable direclty in that layout file it works. But that's not what I want.

How can I solve the problem?

ADDITIONAL INFORMATION:

My menu looks like this

<ul class="nav nav-pills menu">

    {% if section is not defined %} 
        {% set section = '__dummy_undefined' %} 
    {% endif %}

    <li{% if section == 'home' %} class="active"{% endif %}><a href="{{ path('home') }}">    {{ 'general.home'|trans }}</a></li>  
    <li{% if section == 'search' %} class="active"{% endif %}><a href="{{ path('search') }}">{{ 'general.search'|trans }}</a></li>
    <li{% if section == 'events' %} class="active"{% endif %}><a href="{{ path('events') }}">{{ 'general.calendar'|trans }}</a></li>          
    <li{% if section == 'register' %} class="active"{% endif %}><a href="{{ path('fos_user_registration_register') }}">{{ 'general.register'|trans }}</a></li>
</ul> 

So the variable is defined right before it is used. The menu is contained in a menu.html.twig file which is included like that:

{% include 'DancePortalFrontendBundle::headline.html.twig' %}

The include comes BEFORE {% block content %} This should never be displayed! {% endblock %}

But even if i do the include after or in the content block, I do not get the section value that I set in the register_content.html.twig

Does somebody know how to solve that?

1

1 Answers

0
votes

The problem is that your variable in not part of the current scope that you're trying to access. That means that you're defining your variable inside the following block:

{% block fos_user_content %}{% endblock %}

..it will probably not be available in your menu block somewhere else in your template.

When you're trying to access the variable later it's already gone (out of scope). What you could do is use this handy feature:

Per default, blocks have access to variables from outer scopes (http://twig.sensiolabs.org/doc/tags/extends.html)

Define your variable in the main layout or anywhere where it becomes global with respect to the current template:

{% extends 'MyBundle::layout.html.twig' %}

{% block content %}
    {# 
        I did it here but you can really set it anywhere. The point is: 
        From now on the variable exists and will only be deleted when the
        content block closes.
    #}
    {% set hightlighted_section = '' %} 
    {% block fos_user_content %}{% endblock %}
{% endblock %}

I don't know your exact use case but in this example you can set the variable anywhere in your FOSUserBundle content files and use it before the content block ends. If that's not enough, define it on a higher level. If you say you need it in the menu, try setting it one block over your menu block.

The point is where it is defined.. not where you set it.