How to Manage Assets in yii2 using twig in child template? - php

I want to extend my base twig template with some asset bundles and add new assets in child template.
{# main.twig #}
register_asset_bundle('app/assets/AppAsset')
{# child.twig #}
{% extends 'layouts/main.twig' %}
register_asset_bundle('app/assets/NewAsset')
So I got error "A template that extends another one cannot include content outside Twig blocks. Did you forget to put the content inside a {% block %} tag?"
So need I to set each asset to blocks? That is uncomfortable when I use asset bundles. Google have no one solutions for this, what should I do in this situation?

It is required for a child template to have its content strictly in the block tags. The engine substitutes the parent blocks for the blocks defined in the child.
Therefore, your main.twig template should include at least one block, e.g.:
register_asset_bundle('app/assets/AppAsset')
{% block content %}{% endblock %}
So that the child can override it as follows:
{% extends 'layouts/main.twig' %}
{% block content %}
register_asset_bundle('app/assets/NewAsset')
{# More content here #}
{% endblock %}
Whatever you put into the "content" block in the parent template will be replaced with the content of that block defined in the child.

Related

Is there a way, to change a block of a twig template within a controller (php file)

Let's say I have the following twig templates:
{% block header %}My header{% endblock %}
{% block middle %}My middle{% endblock %}
{% block footer %}My footer{% endblock %}
In my controller, I render the twig file (with laravel twig bridge, but it would be similar in other frameworks):
public function index() {
$view = view('template.twig');
return $view->render();
}
So is there a way, to manipulate the $view to set for example the block middle to My new middle, within the controller, without touching the template.twig file?
I know, I could create a new child.twig where I do
{% extends "template" %}
{% block middle %}My new middle{% endblock}
but is there a way to do it, without an extra twig file directly form the controller? Something like
$view->setBlock('middle', 'My new middle');

Render a block of a "parent" template from a "child" template in Twig

I have two templates:
Fields.html.twig, which includes a child template.
{% block important %}
This block should be renderable from the sub template
{% endblock important %}
{% include 'XButton.html.twig' %}
XButton.html.twig, which needs to render blocks defined in the template that includes it.
block('important')
It seems like an included template isn't able to simply render blocks of whoever included it.
How can the parent template pass the full context to the child, so that the child can access the blocks defined in the parent?
What you're trying to do it's impossible, because the included template has no way to know about blocks defined in other templates...
You'll need to structure your code a bit differently, so that the contents of the block are defined in the parent somehow - you have at least a couple of options:
Using set and include: https://twigfiddle.com/l536np
Fields.html.twig
{% set important %}
You define the child content as a variable in the parent...
{% endset %}
{% include 'XButton.html.twig' %}
XButton.html.twig
{{ important }}
Using embed: https://twigfiddle.com/y9pp6p
Fields.html.twig
{% embed 'XButton.html.twig' %}
{% block important %}
You define the block content in the parent...
{% endblock %}
{% endembed %}
XButton.html.twig
{{ block('important') }}
You can do it with include by passing the parent template name
{% block important %}
This block should be renderable from the sub template
{% endblock important %}
{% include 'XButton.html.twig' with {'parent': _self} %}
And then use it in the include template
{{ block('important', parent) }}
Although this way the content of the block will be rendered twice - once when you define it in the parent, and once again in the child - see https://twigfiddle.com/6t0l6p

Difference between customizing form theme and referencing block in Symfony?

what is the difference between referencing a widget block and customizing it : the docs say :
So far, to override a particular form block, the best method is to
copy the default block from form_div_layout.html.twig, paste it into a
different template, and then customize it. In many cases, you can
avoid doing this by referencing the base block when customizing it
But to me it looks the same :
{# app/Resources/views/Form/fields.html.twig #}
{% extends 'form_div_layout.html.twig' %}
{% block integer_widget %}
<div class="integer_widget">
{{ parent() }}
</div>
{% endblock %}
{# app/Resources/views/form/fields.html.twig #}
{% block integer_widget %}
<div class="integer_widget">
{% set type = type|default('number') %}
{{ block('form_widget_simple') }}
</div>
{% endblock %}
What is the difference?
The first example in the documentation that you are referencing shows how to override the entire widget that displays your form element.
The second example in the documentation that you are referencing shows how you can employ code re-use so that you are not rewriting form templating sections that you are not modifying. So, instead of having to declare
{% set type = type|default('number') %}
{{ block('form_widget_simple') }}
all over again in your overriding widget, you can instead reference the base block that already has this. If you are referencing base blocks from an external template, you can call the parent block via {{ parent() }}, and if you are referencing blocks from inside the same template as the form, you can call the base block via {{ block('base_integer_widget') }}
If you look at it from a PHP/Symfony point of view with inheritance that can help explain it as well. Say you have one PHP class that extends another and you want to override a function named doSomething() - you might rewrite the entire function as you need it. But, say that doSomething() has a block of common code that you always want to run, then you might perform your actions and call parent::doSomething() at the end of it. Or, if you're accessing a different Symfony service you might call $this->get('some.service')->doSomething() instead.
That's the same concept here, you can either override the entire widget or you can override parts of it - perhaps putting a surrounding <div></div> but calling {{ parent() }} from within that since you're changing nothing else about the widget.
I do have one example where I overrode standard button behavior in Symfony and used both cases. I have a separate template file in `app/Resources/views/Form/navigationButton.html.twig' with the following code:
{% use 'form_div_layout.html.twig' %}
{% block button_widget -%}
{% set attr = attr|merge({class: (attr.class|default(''))|trim}) %}
{{- parent() -}}
{%- endblock %}
{% block button_row -%}
{{- form_widget(form) -}}
{%- endblock button_row %}
I am overriding the button widget by allowing additional classes to be passed as attributes and then calling the parent widget to produce it as normal. I then override the button row widget to not put surrounding <div></div> tags since I didn't want that in my template (see the originals here and here).
Then to use in one of my templates I simply do:
{% form_theme form ':Form:navigationButton.html.twig' %}

Twig block indirect extending and appending

I have splitted my layout and templates to few partials, mostly because of old Symfony1 habits.
file layout.html.twig:
...
<body>
{{ include("ABCBundle:Partials:breadcrumbs.html.twig") }}
{% block body %}{% endblock %}
</body>
...
file breadcrumbs.html.twig
<div class="abc">
{% block breadcrumbs %}
Home
{% endblock %}
</div>
file show.html.twig
{% extends "ABCBundle::layout.html.twig" %}
{% block breadcrumbs %}
{{ parent() }}
abc
{% endblock %}
{% block body %}
(something something)
{% endblock %}
Funny thing is, when I render show.html.twig, I can put data into body block, and everything works fine, but I can't do anything with breadcrumbs block. Whatever I do - write something inside that block or call parent(), nothing happens, only content from breadcrumbs.html.twig is being rendered. There's also no error about calling parent() and any other error related to extending block.
I think you should use {% include "..." %} rather than {{ include("...") }}
According to the twig documentation, the {% include %} tag "includes a template and returns the rendered content of that file into the current namespace" where as the include function "returns the rendered content of a template"
(Meaning the include function just returns the rendered content, where the tag adds the content to the current namespace which would include the blocks you defined)
Found the solution.
It looks like you can't extend block included in layout's include, from template extending this layout:
[partial.html.twig] --- [ layout.html.twig ] --- [ template.html.twig ]
{% block abc %} {% include 'partial.html.twig' %} {% extends layout.html.twig %}
{% block abc%} aaa {%endblock %}
You have to perform a little hack, including adding extra content to layout and replacing original block from partial.html.twig with conditional. More here: Overriding blocks within included Twig templates

embedding a form view in a parent view

My Symfony app view layout is a three level hierarchy, such that there is the baseview.html.twig and then the child of that is layout.html.twig and then the child of layout view is X,Y,Z,.... .html.twig. I wanted to pass in a form to the layout.html.twig, however where in my controller should I do this?
Here's some code to make it more clear of the hierarchy. In the bottom base line view I have the following:
{% extends 'AppMainBundle::layout.html.twig' %}
and then in layout.html.twig I have:
{% extends '::base.html.twig' %}
so I wanted to pass in a formView to my layout.html.twig, the question is how??
Use blocks like block content in documentation
For Ex:
{% extends '::base.html.twig' %}
{% block content %}
{{form_widget(form)}}
{% endblock %}
Now content block will be replaced in parent by your block.

Categories