N
N
nepster-web2015-12-29 18:43:51
PHP
nepster-web, 2015-12-29 18:43:51

Twig how to make different templates for guest and user?

Faced the following problem, I have a task to make a page that should be inserted into a different layout for the guest and for the user.
I.e. There is, when a guest visits the site, he sees the pages in a certain way, when he logs in, he gets into his personal account and these same pages (only in a certain case, for example, statistical pages) he must see through his personal account.
It looks like this (main template):

...
        {% block pageTitleContainer %}{% endblock %}
        
        {% block slider %}{% endblock %}

        <!-- MAIN CONTENT -->
        <div id="outermain">
        	<div class="container">
        	<section id="maincontent" class="twelve columns">
                {% block content %}{% endblock %}
                <div class="clear"></div><!-- clear float --> 
            </section>
            </div>
        </div>
 ...

It shows all pages for guests. When the user logs in, he should see everything through:
{% extends "@app/themes/basic/layouts/main.twig" %}

{% block title %} Личный Бэк-офис {% endblock %}

{% block content %}
 ....
   {% block office_content %}
                    -----
   {% endblock office_content %}
 ...
{% endblock content %}

To avoid code duplication, I inherited from the main template. Now, in some child templates, I need to check if the user is a guest, then override the content block, and if he is authorized, then override the office_content block.
Tell me how can this be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2015-12-29
@Sanasol

Well, pass the session to the template or some variable that says that you need to display this or that block.
twig.sensiolabs.org/doc/tags/if.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question