symfony - Cant override blocks in included twig template -
please me understand right way use template inheritance in twig.
i have template base.html.twig other templates extend. contains html, head, body, etc. tags , few blocks.
i move < head > section base.html.twig template own file head.html.twig. if use include directive title block in section no longer gets overridden extending templates.
i determined work around setting title variable in extending template , passing in include statement. have pages need bits of javascript loaded in < head > section, have add variables indicate of load...
obviously horrible kludge. right way this?
edit: @ suggestion of @goto tried replacing include embed. code looks like:
{# base.html.twig #} <!doctype html> <html> {% embed 'appbundle::head.html.twig' %} {% block title %}my company name{% endblock %} {% endembed %} <body> {% block content %}{% endblock %} </body> </html> {# head.html.twig #} <head> <title>{% block title %}{% endblock %}</title> <!-- load other css, js, etc --> </head> {# page.html.twig #} {% extends 'appbundle::base.html.twig' %} {% block title %}{{ page.title }} - company name{% endblock %} {% block content %} <h1>{{ page.title }}</h1> {{ page.content|raw }} {% endblock %}
but doesnt work, title "my company name".
okay, example have base.html.twig
<html> <head> <title>{% block title %}{% endblock %}</title> <link rel="shortcut icon" href="{{ someurl }}"> {% block css %} ..... ///some css here {% endblock %} {% block js %} ...//javascript goes here {% endblock %} .......
with twig inheritance can ovverride of these blocks in child this:
child.html.twig
{% extends ::base.html.twig %} {% block js %} {{ parent() }} .... new js {% endblock %}
with key word {{ parent() }} include existing data base template current one, without clear block.
Comments
Post a Comment