Sunday, 15 May 2011

symfony - Cant override blocks in included twig template -


Please help me understand the correct way to use template succession in a twig.

I have a template base.html.twig that expanded all my other templates. It has html, head, body, etc. tags and some blocks

I & lt; Open the file in your own file head.html.twig from the base.html.twig template. If I use the instructions contained in it, then the title block in that section is no longer overridden by extended templates.

I have determined that I set this title in the extended template as a variable and passing it in my included statement but now I have the pages which give additional bits of j & lt; Head> section, so I have to add the variable to indicate which to load ...

Obviously this is a terrible hip. What is the right way to do this?

EDIT: I tried to get the place on the suggestion of Goto, now my code looks like this:

  {# base.html.twig # } & Lt ;! DOCTYPE html & gt; & Lt; Html & gt; {% Embed 'AppBundle :: head.html.twig'%} {% block title%} My company name {% endblock%} {% endembed%} & lt; Body & gt; {% Block content%} {% endblock%} & lt; / Body & gt; & Lt; / Html & gt; {# Head.html.twig #} & lt; Head & gt; & Lt; Title & gt; {% Block title%} {% endblock%} & lt; / Title & gt; & Lt ;! - Load other CSS, JS, etc. - & gt; & Lt; / Head & gt; {# Page.html.twig #} {% AppBundle :: base.html.twig '%} {% block title%} extends {{page.title}} - My company name {% endblock%} {% Block content%} & lt; H1 & gt; {{Page.title}} & lt; / H1> {{Page.content | Raw}} {% endblock%}  

But this does not work, the title is always "My company name".

OK, for example you have base.html.twig < / P>

  & lt; Html & gt; & Lt; Top & gt; & Lt; Title & gt; {% Block title%} {% endblock%} & lt; / Title & gt; & Lt; Link rel = "shortcut icon" href = "{{Someurl}}" & gt; {% Block css%} ..... /// Some CSS here {% endblock%} {% block js%} ... Javascript goes here {% endblock%} .......  

With trigger legacy you can overwhelm any block from your child in this way:

child.html.twig

  {% extended :: base.html.twig%} {% block js%} {{guardian ()}} .... your new js {% endblock%}  < With the key word  {{parent ()}}  you can add existing data from the base template to your present, without it you will be able to Will lock. 


No comments:

Post a Comment