Here is my template architecture :
list.html.twig => question.html.twig => tagList.html.twig => tagBadge.html.twig
In my tag list, I have a tag variable that is well recognized, but in the tagBadge that is included, I get the error Variable tag does not exist.
I don't know what is the origin of the error. Maybe Twig can't follow the track of variables after a certain depth of inclusion ? I checked for invisible characters in my files. I verified my syntax again and again, but maybe my error is simple...
tagList.html.twig
{% for tag in tags %}
{{tag.name}}{# tag is recognized #}
{% include 'tag/partials/tagBadge.html.twig' with {'tag': tag} %}
{# tag is not recognized in template #}
{% endfor %}
tagBadge.html.twig
{{tag.name}}
My question: why tag is not recognized in my second template 'tagBadge'. I'm working with Symfony 4.2.
Ok, I found this week end the mistake I did. It appeared that I used my template in to different places of my view. It wasn't that use of the template which throw the error, but another I totally forgot, to which I didn't pass the variable... I feel dumb ;) let my mistake be useful for others !
Related
I want to customize the way an entry of a CollectionType in a form will be rendered, that is the way the prototype is generated, I need my own HTML. So I need to do it with the Twig form_theme, but it doesn't work.
I am currently using Symfony 4.1.2.
Here is what I have for now:
In the template that renders the whole form:
{% form_theme form.additionalEmails.vars.prototype 'form/additionalEmail.html.twig' %}
In the form/additionalEmail.html.twig file:
{% block form_row %}
<div style="margin: 10px 0">
{{ form_label(form) }}
{{ form_widget(form.children.value) }}
<div>Recevoir les mails de type : {{ form_row(form.children.isUsedForAdminCommChannel) }}{{ form_row(form.children.isUsedForInfosPratiquesCommChannel) }}{{ form_row(form.children.isUsedForAgendaCommChannel) }}</div>
</div>
{% endblock %}
If I do that I get this error:
Key "value" does not exist as the array is empty.
What is weird is that if I replace the above code by this (by commenting the old one and adding the new line):
{{ dump(form.children) }}
I see that form.children is an array with 4 items and one named "value" so it is clearly broken.
Your help would be much appreciated, as I'm just an amateur dev trying to make a website for a youth movement ^^
Best Regards,
[EDIT]
Here is the form that includes the CollectionType:
https://gist.github.com/MrPOC/a4af94cc5e577a1244dac59de5ea3506
The field is named "additionalEmails"
Just so I understand you correctly. You have a Form which contains a CollectionType and you want to change the prototype of that CollectionType?
If yes could you please post the Form with the CollectionType?
EDIT: Ok so here's how to overwrite the prototype template for a CollectionType.
First we have to find out what your forms unique_block_prefix is. You can find this by going to the page where this form is rendered, you then click in the symfony debug bar on the form icon. Then you have to expand the View variables block and look for unique_block_prefix.
I assume your block prefix could look like this _user_type the name of the block you would overwrite would then be _user_type_additionalEmails_entry_widget (for more details please read the link provided below)
You can replace the widget part with row or label depending on what you want to overwrite (I would suggest trying to overwrite them all to see what exactly changes because I don't know of the top of my head)
In twig you would then take your block prefix and overwrite the prototype field in the same way you would overwrite any other field
{% block '_user_type_additionalEmails_entry_widget' %}
//Content
{% endblock %}
What I wrote down here is what I found out after reading https://github.com/symfony/symfony-docs/issues/6056 (read HeahDude' comment specifically)
I hope this helps!
In my twig view files for a Symfony application I need to write a log {% trans %}Foo Bar Baz{% endtrans %} what is quite annoying.
Therefore I tried to make a template, where I just have to write trans press space and the magic is done.
I made a new Template in Twig File, Twig Block and Twig Variable but non of them worked. The Code I used was:
{% trans %}${TEXT}{% endtrans %}
Abbreviation is trans
Then I restarted NetBeans, but unfortunately nothing happens when I write trans in a .twig file.
What am I doing wrong? How do I have to do this?
Thanks (:
You should add your Code Template in the Twig Block language, not Twig File language.
You don't need to reboot netbeans.
Then, type relatively quickly: {% trenter
You'll come up with:
If you don't want to type {% at all, you can choose HTML language instead of Twig Block language (assuming your file name ends with .html.twig).
I am desperately trying to render a submenu in symfony-cmf.
Example
Structure:
page1
├─p1-subpage1
├─p1-subpage2
└─p1-subpage3
page2
├─p2-subpage1
└─p2-subpage2
Whenever the current page is somewhere within the page1 hierarchy it should use p1-subpage* to render the menu, when I am within the page2 hierarchy it should use p2-subpage* to render the menu. Technically that means it should set the current item to the parent of the 1st level (if it's not already on it) and render one level of nodes (e.g. knp_menu_render('main', { depth: 1 })).
The problem can be split in two parts:
Rendering a (sub-)menu from a given node
Retrieving the current node
Thoughts and Trials
TWIG: It has been suggested to support rendering submenus as a functionality of the KnpMenu itself, but it hasn't been done. As a workaround registering a twig extension has been provided by someone in the issue. However this extension is based on the getCurrentItem Method which has been removed with KNP-Menu 2.0. Although the cmf currently uses v1.1 of the knp-menubundle, this is going to change soon
TWIG: The CnertaBreadcrumbBundle would bring back this functionality, but depends on KNP-Menu 2.0 as well.
TWIG: Using a hack similar as suggested here. It checks the current URI, counts the number of slashes and decides based on that what to use. This could probably work. Problem here: I don't have cmfMainContent variable defined, nor can I find anything similar in my {{ dump() }} (nothing containig a menu either).
RouteVoter: The cmf itself has some MenuVoters itself, which are well documented what they are, but not how to use them. I don't think that there is any way to access that functionality whithin twig nor do I know how to intercept the menu building.
Thanks for any help.
have a look here for an example of using voters to make decision about what to highlight:
https://github.com/dbu/conference-tutorial-1.0/pull/20
aside from this we are making good progress on a KnpMenu 2.x compatible version of our MenuBundle but it might be until January until we make a stable release of it (but we might make one earlier .. we will see):
https://github.com/symfony-cmf/MenuBundle/pull/214
I have created a bundle yesterday for my own, similiar, use case.
However as all of my pages share the same route you might need to adapt it quite a bit.
I still think you might find some inspiration, especially for the second part of your problem.
My Bundle:
https://github.com/burki94/RecursiveMenuBundle/blob/master/README.md
AbstractRecursiveBuilder: https://github.com/burki94/RecursiveMenuBundle/blob/master/Menu/AbstractRecursiveBuilder.php:
This is not really a solution because it's not following my requirements of being compatible with KnpMenu 2.*. But this deprecated solution is easy:
{% set currentItem = knp_menu_get('main').currentItem %}
{% if currentItem is not null %}
{% if currentItem.getLevel() == 1 %}
{% set main = currentItem %}
{% else %}
{% set main = currentItem.getParent() %}
{% endif %}
{{ knp_menu_render(main, { 'template': 'ComBundle:Default:left_menu.html.twig', 'currentClass': 'uk-active' }) }}
{% endif %}
Does anybody know how to get the current URI in a Twig template?
I’ve read through the documentation and I’m unable to find the Twig function to do this.
{{ app.request.uri }}
If you want to read it into a view variable:
{% set uri = app.request.uri %}
The app global view variable contains all sorts of useful shortcuts, such as app.session and app.security.token.user, that reference the services you might use in a controller.
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.