Twig: Multiple title blocks

Created on 27 Jan 2015  路  9Comments  路  Source: twigphp/Twig

I'd like to use multiple title blocks. Only one block is active depending on if statement.

        {% if app.request.get('_route') == 'dashboard_homepage' %}
            {% block title %}Open Tickets{% endblock %}  <-- Line 7
            <h1 class="page-header list-group">Open Tickets</h1>
        {% endif %}
        {% if app.request.get('_route') == 'dashboard_archive' %}
            {% block title %}Archived Tickets{% endblock %} <-- Line 11
            <h1 class="page-header list-group">Archived Tickets</h1>
        {% endif %}

Error:

The block 'title' has already been defined line 7 in DashboardBundle:Default:dashboard.html.twig at line 11

Most helpful comment

You're not able to define a block twice - we normally handle such cases like this:

{% set title = app.request.get('_route') == 'dashboard_homepage' ? 'Open Tickets' : 'Archived Tickets' %}

{% block title %}{{ title }}{% endblock title %}
<h1 class="page-header list-group">{{ title }}</h1>

For what its worth, it doesnt make much sense for route names to define the page titles - why cant the inheriting template set the title?

All 9 comments

You're not able to define a block twice - we normally handle such cases like this:

{% set title = app.request.get('_route') == 'dashboard_homepage' ? 'Open Tickets' : 'Archived Tickets' %}

{% block title %}{{ title }}{% endblock title %}
<h1 class="page-header list-group">{{ title }}</h1>

For what its worth, it doesnt make much sense for route names to define the page titles - why cant the inheriting template set the title?

Mmm instead of put logic inside a template, you can simply define title variable in the controller.

Responsibility for the title is in the view, not the controller.

@merk Thanks for tip. I also think that it is not good idea to place title in controller. So what will be best practice to build titles?

We implement a global twig variable that contains an array of breadcrumbs and calculate titles based on the breadcrumbs:

https://github.com/infinite-networks/InfiniteCommonBundle/blob/master/Twig/SiteGlobal.php#L57

Our base template might contain something like this:

    {% block page_title %}
    {% if not noRootBreadcrumb | default(false) %}
    {% do site.addBreadcrumb(site.shortName, path('dashboard')) %}
    {% endif %}
    <title>{% for crumb in site.breadcrumbs %}{{ crumb[0] }}{% if not loop.last %} &mdash; {% endif %}{% endfor %}</title>
    {% endblock page_title %}

{# ... #}

{% block body_header %}<h3 class="page-title">{{ site.breadcrumbs[0][0] }}</h3>{% endblock body_header %}

And a template extending the base template would have a call like:

{% extends "::base.html.twig" %}

{% set abn = data.abn %}
{% do site.addBreadcrumb(abn.name | default(abn.abn | abn_format)) %}

It allows a hierarchy of templates to add breadcrumbs, and the last added breadcrumb becomes the body title

@merk I have small project so I think optimal solution will be to use technique from your first comment here. I modified it a little but have an error. I also discussed this error on SF's IRC chat. If line 11 removed rest of code is working http://joxi.ru/YmEp5y0u9jogr6 http://joxi.ru/krDkQq0iL3yZAp http://joxi.ru/xAeXoYOh6Z8Z2y . Can you please check my templates and error here http://pastebin.com/8HHbTRWA ?

Closing as there is no "bug" in Twig. @SergeC If you still have problems, can you use the mailing-list? Thank you.

i need your help please:

{% if encuesta is defined %}
{% block content %}{% endblock %}
                <div class="col-sm-12 col-md-3">
                    {{ encuesta }}
                </div>
            </div>
        {%else%}
            {% block content %}{% endblock %}
        {%endif%}

    </div>
{% block content %}
    {% if encuesta is defined %}
        <div class="col-sm-12 col-md-3">
            {{ encuesta }}
        </div>
    {% endif %}
{% endblock %}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

maidmaid picture maidmaid  路  5Comments

dvladimirov77 picture dvladimirov77  路  5Comments

SDPrio picture SDPrio  路  3Comments

rungta picture rungta  路  3Comments

yguedidi picture yguedidi  路  4Comments