ReynierPM
ReynierPM

Reputation: 18660

Template hierarchy at Twig and Symfony2

I'm building a /app/Resources/base.html.twig with common features for almost all the development I have since for example all use Twitter Bootstrap, Select2 and some common libraries. Then for any project I created a bundle called TemplateBundle and inside it I have a template called layout.html.twig which extends from base.html.twig. Having this as a start point I have some doubts around my approach:

I want to make base.html.twig as extendable as can be and in the code above not all of my sites uses datepicker.css or styles.css since this are part of the current one or in other cases files names changed between sites, so my question regarding this is: how I handle this in layout.html.twig? For example in Site1 all the templates will extends from layout.html.twig and then in this layout I can set the use of datepicker and styles but for Site2 I could define to use just styles and so on, so how to handle this in layout.html.twig maintaining assetic?

Understand my question?

Upvotes: 0

Views: 568

Answers (1)

Billiam
Billiam

Reputation: 34

The base template will be very complicated. I will rather to have multi layout instead. Since you have everything in a bundle.

Another option is use have the common ones in base.html.twig. and extends at layout.html.twig with {{ parent() }} which will print the base.html.twig block content, but you will lose the front end performance in production.

base.html.twig

{% block stylesheets %}
    {% stylesheets 'bundles/template/css/bootstrap.min.css' filter='cssrewrite' %}
    <link rel="stylesheet" href="{{ asset_url }}" />
    {% endstylesheets %}
{% endblock %}

layout.html.twig

{% block stylesheets %}
    {{ parent() }}
    {% stylesheets 
        'bundles/template/css/datepicker.css' 
        'bundles/template/css/datepicker3.css'
    filter='cssrewrite' %}
    <link rel="stylesheet" href="{{ asset_url }}" />
    {% endstylesheets %}
{% endblock %}

Upvotes: 1

Related Questions