template twig header and footer always visible without using {% block [...]%}

1

I'm doing a website for me and I have a question about how to use the templates. That is, right now I use {% block [...] %} to add my header and footer to the pages. But I would like to make both header and footer always present on the page without having to invoke them on each page.

    
asked by Pavlo B. 01.07.2018 в 19:01
source

1 answer

1

You have two options:

  • Put the content of the header and the footer in the base template without putting it inside any block tag. With this you will always leave without having to invoke it, the problem is that you can not remove it in any template that inherits from it. If you are not going to remove it, this may be the best option.

  • Put in the base template the header and footer content inside block tags but do not invoke those blocks from the child template. In this way they will always come out, but if, in addition, in a child template you want nothing to appear in any of the blocks, you invoke it empty. And if you want them to appear but with other content, you invoke them with that content that will then replace the base template.

  • I'll give you two examples to make it clearer:

    {# Caso 1 - Plantilla base: base.html.twig #}
    <html>
      <head>
      ...
      </head>
     <body>
      <nav>
        <li>Inicio</li>
        <li>Blog</li>
        <li>Acerca de</li>
      <nav>
      {% block content %}
      {% endblock %}
      <footer>
       ...
      </footer>
    </body>
    </html>
    
    {# Plantilla hija #}
    {% extends "base.html.twig" %}
    {% block content %}
    <h1>Mi blog</h1>
    <p>Lorem ipsum, lo otro y el de la moto</p>
    {% endblock %}

        {# Caso 2 - Plantilla base: base.html.twig #}
        <html>
          <head>
          ...
          </head>
          <body>
          {% block header %}
           <nav>
             <li>Inicio</li>
             <li>Blog</li>
             <li>Acerca de</li>
           <nav>
          {% endblock %}
         
         {% block content %}
         {% endblock %}
         
         {% block footer %}
          <footer>
           ...
          </footer>
         {% endblock %}
    
        </body>
        </html>
    
    
        {# Plantilla hija - Como ves aquí todo es igual#}
        {% extends "base.html.twig" %}
        {% block content %}
        <h1>Mi blog</h1>
        <p>Lorem ipsum, lo otro y el de la moto</p>
        {% endblock %}
        
    answered by 01.07.2018 / 20:30
    source