Nll Nll - 1 month ago 11
Twig Question

How to change the title of page dynamically with symfony2

Im using Twig for template in my website,I extend layout.html.twig in all my others twig pages,so In my layout.html.twig I have the title of page :

<title>{% block title %}Title of my website{% endblock %}</title>


Now my problem is how to change this title dynimacally in all my pages,for example I have news.html.twig to show all latests news in the world,so I hope when I display my news page I have the title of the news in my title of page...

<title>{% block title %}Title of the news{% endblock %}</title>

Answer

You are close. In your news.html.twig I assume you have all of your content in a block like this:

{% extends '::layout.html.twig' %}

{% block content %}
    content of the news page here
{% endblock %}`

So all you have to do is add another block for the title outside of that content block

{% extends '::layout.html.twig' %}

{% block title %}Title of news page{% endblock %}

{% block content %}
    content of the news page here
{% endblock %}`
Comments