php - xTrying to include a header block in Sympfony 3

385

I am totally new to this. I took as an example the body block.

This is my base.html.twig file content:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>{% block title %}Welcome!{% endblock %}</title>
        {% block stylesheets %}{% endblock %}
        <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
    </head>
    <body>
        {% block header %} {% endblock %}
        {% block body %}{% endblock %}
        {% block footer %}{% endblock %}
        {% block javascripts %}{% endblock %}
    </body>
</html>

and this is my header from app\Resources\views\header\header.html.twig content:

{% extends 'base.html.twig' %}
{% block header %}
Header HeaderHeaderHeaderHeader
{% endblock %}

But this is not working for some reason. Do i need to do smth more ? thx

[UPDATE]

I attached an image to see what i want to achieve: enter image description here

This is folder structure: enter image description here

[UPDATE]

The content of the index.html.twig file is:

{% extends 'base.html.twig' %}

{% block body %}
    <div id="wrapper">
        <div id="container">
          afdsfsdfsfasddf
        </div>
    </div>
{% endblock %}

{% block stylesheets %}
<style>
    body { background: #F5F5F5; font: 18px/1.5 sans-serif; }
</style>
{% endblock %}
36

Answer

Solution:

I suggest you to do that:

base.html.twig

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>{% block title %}Welcome!{% endblock %}</title>
        {% block stylesheets %}{% endblock %}
        <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
    </head>
    <body>
        {# header section #}
        <div>
             {% include ':header:header.html.twig' %}
             {% block header %} {% endblock %}
        </div>            

        {# body section #}
        <div>
             {% block body %}{% endblock %}
        </div>

        {% block footer %}{% endblock %}
        {% block javascripts %}{% endblock %}

    </body>
</html>

header.html.twig

Remove this section

{% extends 'base.html.twig' %}
{% block header %}
Header HeaderHeaderHeaderHeader
{% endblock %}

And make only the header content

index.html.twig

{% extends 'base.html.twig' %}

{% block body %}
    <div id="wrapper">
        <div id="container">
          afdsfsdfsfasddf
        </div>
    </div>
{% endblock %}

{% block stylesheets %}
<style>
    body { background: #F5F5F5; font: 18px/1.5 sans-serif; }
</style>
{% endblock %}

People are also looking for solutions to the problem: Get item corresponding to another item from json in php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.