How to change the title of page dynamically with symfony2
My solution looks a bit more elegant in the child-templates:
In your layout.html.twig
:
<title>{% if page_title is defined %} {{ page_title }} | {% endif %} Your Website</title>
In your child page-templates:
{% extends '::layout.html.twig' %}
{% set page_title = 'Your page-title' %}
{# Put the rest of your page below #}
And you can also reuse the title in eg. an doing <h1>{{ page_title }}</h1>
:-)
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 %}`
Is more simple that you imagine.
Put on main/parent layout
...
<title>{% block title %}{% endblock %} - My Site</title>
...
and, in a content page ( on child layout ), get the page title
...
<h1>{{ block('title') }}</h1>
...
So, any title that you put in head title will be displayed in the content title.