how does "{% use %}" work in twig using symfony3?

gabtzi

I'm trying to use the {% use %} syntax to import the blocks from a child template in my base template as described in http://twig.sensiolabs.org/doc/tags/use.html but I'm having some issues so I'm wondering what I'm missing.

I have the following files

#base.html.twig
<!DOCTYPE html>
<html>
    {% use ':admin/page:body.html.twig' %}
</html>

#admin/page/body.html.twig
{% block contentBody %}
    <body>
        <p>Test Body</p>
    </body>
{% endblock %}

and finally

#default/index.html.twig
{% extends base.html.twig %}

{% block contentBody %}
   <body>
      <p>Test ContentBody</p>
   </body>
{% endblock %}

As I understand it according to the documentation the first two files are almost as if I declared one

#base.html.twig
<!DOCTYPE html>
<html>
{% block contentBody %}
    <body>
        <p>Test Body</p>
    </body>
{% endblock %}
</html>

My controller renders default/index.html.twig

Using the first 3 files together yields a blank page although I would think it should render "Test ContentBody"

If I replace the base.html.twig contents with the last snippet it works as it should and renders "Test ContentBody"

If I replace the use command with include it yields "Test Body" as expected

If anyone could explain to me what I'm doing wrong or steer me in the right direction as to what I'm missing in the documentation I would really appreciate it. Thanks in advance

PS: I've also tried to just declare the block in admin/page/body.html.twig like this

#admin/page/body.html.twig
{% block contentBody %}{% endblock %}

in case it breaks because of the part in the documentation that says when you "use" templates they should not have a body but it didn't help.

NDM

Importing blocks with use does not output them automatically, so you still have to define them in your base template:

#base.html.twig
<!DOCTYPE html>
<html>
    {% use ':admin/page:body.html.twig' %}
    {% block contentBody %}
        parent()
    {% endblock %}
</html>

the use is mostly intended for reusing blocks with the block() function.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How Does XML::Twig keep_encoding work?

From Dev

Twig extension filter or function name in twig variable does not work?

From Dev

How to render TWIG output to a variable for later use (symfony2)?

From Dev

How the twig caching exactly work?

From Dev

How to enable autoescaping in Twig using Symfony

From Dev

How does HTTPS work, and does it use cookies?

From Dev

How do I format time to hh:mm using Twig (Symfony)

From Dev

Does Twig "is defined" work with iterable objects?

From Dev

How to access a global service function within a twig extension using Symfony?

From Dev

How to use global variable in symfony twig file?

From Dev

Symfony3: How to update boolean to false using PATCH method

From Dev

Twig does not update changes in symfony

From Dev

Symfony variable does not exist in twig

From Dev

How use rendered variables in a foreach Symfony 2 ~ twig

From Dev

how does "{% use %}" work in twig using symfony3?

From Dev

using ckeditor in symfony3

From Dev

How does HTTPS work, and does it use cookies?

From Dev

How use rendered variables in a foreach Symfony 2 ~ twig

From Dev

Symfony3 report generation (html) of PHPUnit does not work

From Dev

How to make assets work with Symfony3 in subdirectory on Nginx

From Dev

using ckeditor in symfony3

From Dev

Symfony3 update form does not work

From Dev

symfony3 twig and show session array

From Dev

How to use Grouped Use statements with Symfony3?

From Dev

IvoryCKEditorBundle Symfony: How to use "ckeditor_widget" from IvoryCKEditorBundle TWIG template in my OWN redefined TWIG template

From Dev

How to use redirect users to different sub-website based on user information based on symfony3

From Dev

Meta tag from head goes into body on server using twig template symfony3

From Dev

Symfony3 service not found by twig

From Dev

how does scope work when using property in Python 3?

Related Related

  1. 1

    How Does XML::Twig keep_encoding work?

  2. 2

    Twig extension filter or function name in twig variable does not work?

  3. 3

    How to render TWIG output to a variable for later use (symfony2)?

  4. 4

    How the twig caching exactly work?

  5. 5

    How to enable autoescaping in Twig using Symfony

  6. 6

    How does HTTPS work, and does it use cookies?

  7. 7

    How do I format time to hh:mm using Twig (Symfony)

  8. 8

    Does Twig "is defined" work with iterable objects?

  9. 9

    How to access a global service function within a twig extension using Symfony?

  10. 10

    How to use global variable in symfony twig file?

  11. 11

    Symfony3: How to update boolean to false using PATCH method

  12. 12

    Twig does not update changes in symfony

  13. 13

    Symfony variable does not exist in twig

  14. 14

    How use rendered variables in a foreach Symfony 2 ~ twig

  15. 15

    how does "{% use %}" work in twig using symfony3?

  16. 16

    using ckeditor in symfony3

  17. 17

    How does HTTPS work, and does it use cookies?

  18. 18

    How use rendered variables in a foreach Symfony 2 ~ twig

  19. 19

    Symfony3 report generation (html) of PHPUnit does not work

  20. 20

    How to make assets work with Symfony3 in subdirectory on Nginx

  21. 21

    using ckeditor in symfony3

  22. 22

    Symfony3 update form does not work

  23. 23

    symfony3 twig and show session array

  24. 24

    How to use Grouped Use statements with Symfony3?

  25. 25

    IvoryCKEditorBundle Symfony: How to use "ckeditor_widget" from IvoryCKEditorBundle TWIG template in my OWN redefined TWIG template

  26. 26

    How to use redirect users to different sub-website based on user information based on symfony3

  27. 27

    Meta tag from head goes into body on server using twig template symfony3

  28. 28

    Symfony3 service not found by twig

  29. 29

    how does scope work when using property in Python 3?

HotTag

Archive