M
M
Max2019-10-12 21:23:00
symfony
Max, 2019-10-12 21:23:00

How in symfony 3.4 to display the login (or any data) in the base template?

Hello! When creating routes in the controller, I can pass the variables I need to the template I need:

//...

        return $this->render('@App/some.html.twig', [
            'some_key' => $some_var,
        ]);

//...

The template itself extends another @App/layout.html.twig.
{% extends '@App/layout.html.twig' %}

{# ... #}

{% block body %}

    {{ some_key }}

{% endblock %}

{# ... #}

How can I pass the variables I need to @App/layout.html.twig ? For example user login or other kind of data?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2019-10-13
@maximrabotaet

1. Rendering a controller inside a template
https://symfony.com/doc/3.4/templating/embedding_c...

{{ render(controller(
    AppBundle:Article:recentArticles',
    { 'max': 3 }
)) }}

2. Custom Twig Extension that adds a function that implements the desired logic
https://symfony.com/doc/3.4/templating/twig_extens...
3. If you need to display something simple, for example, the application version, then you can use global variables
https://symfony.com/doc/3.4/templating/global_vari...
4. Some things are available in a global variable app, for example, the same instance of the logged in user
This is true for all versions of Symfony

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question