By nbg15


2019-06-12 07:36:11 8 Comments

I have a twig structure like this:

{% extends '@layouts/default.html.twig' %}

{% block headerBar %}
    {% include 'partials/my-navigation.html.twig' %}
{% endblock %}

{% block content %}
    some content
{% endblock %}

But now I have always the same title in my "my-navigation.html.twig".

How can i make it dynamically? maybe something like this:

{% extends '@layouts/default.html.twig' %}

{% block myNiceTitle %}
    COOL TITLE 
{% endblock %}

{% block headerBar %}
    {% include 'partials/my-navigation.html.twig' %}
{% endblock %}

{% block content %}
    some content
{% endblock %}

and then i would use my title in the "my-navigation.html.twig" file dynamically? but how?

I have already read the manual here:

https://twig.symfony.com/doc/2.x/functions/block.html

but it doenst make me smarter :(

Thx for any hints

2 comments

@jeroen 2019-06-12 07:47:11

Assuming that myNiceTitle is a block in your navigation template, you could use embed and do something like:

{% extends '@layouts/default.html.twig' %}

{% block headerBar %}
    {% embed 'partials/my-navigation.html.twig' %}  
        {% block myNiceTitle %}
            COOL TITLE 
        {% endblock %}
    {% endembed %}
{% endblock %}

{% block content %}
    some content
{% endblock %}

And somewhere in partials/my-navigation.html.twig:

{% block myNiceTitle %}
    DEFAULT TITLE {# or nothing at all... #}
{% endblock %}

@nbg15 2019-06-12 07:51:50

could you also tell me how the syntax should be in my navigation template please? Thx

@jeroen 2019-06-12 07:52:08

@nbg15 I had just added that :-)

@nbg15 2019-06-12 07:56:54

so sorry but this will not give me my COOL TITLE as dynamic title in my navigation twig (only 'DEFAULT TITLE')

@nbg15 2019-06-12 08:10:34

works fine :) my stupid head was refreshing the wrong page :( thx man!

@Arleigh Hix 2019-06-12 07:43:47

Set a variable before the include

{% set title = 'COOL TITLE' %}

{% block headerBar %}
  {% include 'partials/my-navigation.html.twig' %}
{% endblock %}

Then use it inside your include

<title>{{ title }}</title>

@nbg15 2019-06-12 07:45:50

Thx for your response, but it get this error: Variable "title" does not exist.

@nbg15 2019-06-12 07:53:57

sorry but i still get the errormessage: Variable "title" does not exist.

Related Questions

Sponsored Content

1 Answered Questions

Twig templates extending

8 Answered Questions

[SOLVED] How to render a DateTime object in a Twig template

11 Answered Questions

[SOLVED] How to concatenate strings in twig

3 Answered Questions

[SOLVED] Extend Twig template without blocks

  • 2016-04-27 06:13:31
  • barbushin
  • 631 View
  • 1 Score
  • 3 Answer
  • Tags:   twig

4 Answered Questions

[SOLVED] Is there a way to include template and hide some of his block

  • 2012-05-21 13:15:32
  • ferdinandfly
  • 5989 View
  • 0 Score
  • 4 Answer
  • Tags:   symfony

1 Answered Questions

Inclusion and scope in Twig templates

  • 2015-07-09 13:32:30
  • AFA Med
  • 103 View
  • 0 Score
  • 1 Answer
  • Tags:   symfony twig

1 Answered Questions

1 Answered Questions

[SOLVED] Twig Template on Symfony Exception Page not rendering head

  • 2014-04-29 13:42:35
  • Julien Leray
  • 728 View
  • 1 Score
  • 1 Answer
  • Tags:   symfony twig

3 Answered Questions

[SOLVED] Twig: How to display html content in the block title

2 Answered Questions

[SOLVED] Symfony2 Twig Inject additional blocks

Sponsored Content