How to extends dynamic template in Twig? - symfony

i need to extends different base template in twig. this is possible? in my code i use dinamically render template with a controller like this
{{ render(controller('AppBundle:Default:menuManager')) }}
in that controller i use one action to choose the right template to render in my page, and this work fine. But in this case is different: i what change my base default (so i presume to extends it, right?) but i don't know how do this. Something like this?
{{ extends(controller('AppBundle:Default:baseManager')) }}
But this code don't work. Is possible? There is a different way? Thanks

This depends on the conditions for choosing the template base but you can use a twig extension. I use that in one project :
{% extends app.request.host | switchBaseTemplate %}
In this example, I use the host for the condition.
You can then write a twig extension easily, as explained here :
http://symfony.com/doc/current/templating/twig_extension.html
Have good dev.

Related

Symfony 3 EasyAdminBundle Collection Type override twig

I have Entity Product with productImages. ManyToOne relationship.
I want to override product images twig. To do it, I created easy_admin folder under view and inside ProductImage folder, then edit and new twigs for test.
It is not overriding at all. Product is overridden easily is this way, but ProductImage is not.
Don't you think in theory it should work like this?
If not, please give me some clue how to override, change collection type twig.
I want to have nice bootstrap table instead current one.
Thanks
If anyone is curious how to override collection type:
in config.yml add:
easy_admin:
design:
form_theme: '/custom_form_layout.html.twig'
Then in custom_form_layout.html.twig write:
{% extends 'EasyAdminBundle:form:bootstrap_3_horizontal_layout.html.twig' %}
Or extend your base form whatever it is.
after that override block
{% block collection_row %}<div>I Love Symfony! (or just use parent())</div>{% endblock collection_row %}

fosuserbundle override template css

I'm trying to override the FOSUserBundle Templates as show here:
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_templates.md
I used the second method, I created my own bundle and put the templates in
myBundle/Resources/Views/Security/login.html.twig
and
myBundle/Resources/Views/Registration/register.html.twig.
both templates start with:
{% extends "::nologbase.html.twig" %}
where nologbase.html.twig is defined in app/Resourcer/views.
well, the first one (login) is correctly displayed but the second one (register) shows row html with no css.
I cleared the cache and I also tried to display only the extended template and compared the source code. it is exactly the same!
any idea?
thanks

Sonata Block Bundle SimpleBlock rendering

I'm using SonataBlockBundle in my current project.
I create a SimpleBlock and render it in my twig template with something like
{{ sonata_block_render('name' : 'myBlock') }}.
But the block consists of title and body, is there a way to render title and body separately, like I can do with form fields?
Thank you in anticipation!
The best way to go there would be to create a custom BlockService (see http://sonata-project.org/bundles/block/master/doc/reference/your_first_block.html for instructions about that), with a custom twig template where you will be able to specify your title/body rendering.

TWIG variables between templates?

Beeing completely new to TWIG (it's also my first templating language) I have a little problem understanding the variables.
Heres what I need:
I have 2 layouts. One inheriting from the other.
On the first one I need to put an if on a div to add a class if on the second layout a variable is declared.
Thanks in advance.
Say in Symfony you write:
return $this->render('::index.html.twig', array('variable' => $somevar));
And 'index.html.twig' looks like this:
{% extends '::foo.html.twig' %}
{# some contents #}
and 'foo.html.twig' looks like this:
{{ variable }}
It should just work. If it doesn't work, post some code and errors here, and I'll see what I can do to help. Obviously this example is unrealistic, but all templates should have access to all variables passed from Symfony, in addition to the global ones and anything you define as Twig extensions.

How to reference twig for custom field types in dedicated bundle?

I am (still) trying to introduce http://xoxco.com/clickable/jquery-tags-input into a dedicated bundle. As far, I have a type as a child of text and a data transformer that converts comma-separated strings into arrays of Objects and vice versa.
Now I want to decorate the text field with the JQuery code linked above. As far as I understand, I have to define a block like
{% block manytomanycomboselector_widget %}
{% spaceless %}
{{ block('text_widget') }}
<script>
$(function(){
$("#{{ id }}").tagsInput();
});
</script>
{% endspaceless %}
{% endblock manytomanycomboselector_widget %}
in [MyTypeBundle]Resources/views/Form/fields.html.twig
Now, both the documentation and the answers for this question at StackOverflow state that I have to reference fields.html.twig somewhere either in the template that uses the form or in app/, but this doesn't seem to be necessarily for other field-type bundles, though I cannot see in their code why.
What do I have to configure inside the bundle besides this block in this file?
Also I didn't get where I have to put the css and js requirements for the header and how I deal with general requirements like jQuery itself.
I have the same issue & I resolve it by merging my field template in the twig.form.resources parameter.
So, in the DI extension of my bundle (MyBundle/DependencyInjection/MyBundleExtension.php), I add:
$container->setParameter('twig.form.resources', array_merge(
array('MyBundle:Form:field_widget.html.twig'),
$container->getParameter('twig.form.resources')
));
Be aware, your bundle must be registered after the TwigBundle in your AppKernel.
EDIT:
A form field is not linked to any JS or CSS. So, IMO, you have 2 solutions.
Firstly, you directly wrap your JS & CSS in your field template and your bundle stays stand-alone.
Secondly, you instruct final users that they need to include manually some JSS & CSS each time they use your field type.
The IoFormBundle & GenemuFormBundle uses the second solution like explain in their documentation.

Resources