In a form, I dynamically define the fields in a controller. Then in the TWIG I would like to use form_widget to output the fields.
The normal approach, when knowing the fields is like shown bellow:
{{ form_widget(form.field1) }}
In this case, we cannot know whether we will have a field1, field2, etc. on beforehand. Though we have the field names in a variable called key.
So what we would like to achieve is replace the hardcoded field1 by something dynamic.
You can do something like
{{ form_widget(form[key]) }}
Related
We're working on twig to merge multiple values. The idea is to have multiple twig templates each containing the value of it's language.
The problem is the values contain handlebars conditions like {{#equals language "nl_NL"}} that causes trouble in twig.
Is it possible to show all contents raw?
I want my project to be filter by the score and I am using a elasticsearch. now, the score have two variables: matchingScore and personalityScore. this two is should be added in the twig to get the actual score. Now, I dont know how to sort an integer since this is not an array and the sorting in the twig is required an array. This is the sample code:
{% for provider in matches | sort matchingScore[provider.id] + personalityScore[provider.id]('desc') %}
{% if matchingScore[provider.id] + personalityScore[provider.id] >= score_criteria %}
Please Help me. thanks.
I'm trying build a custom filter based on a string field. In my case I try map a field "nome_setor" in other table. If I try use variable with a text field works fine:
If I change this for a custom field using another table not works. In this case not show the filter:
Thank you in advance.
You can't use custom fields when you compare it values with some other column.
For you case to work, remove the table alias from the custom field you gonna use, and define your conditional snippet like so:
[[AND {{hithere}}]]
Now when you select the custom field, it should work. But you LIKE won't.
Hope it helps
I'm developing a site with Drupal 8. I made a custom content type, with a lot of fields; in particular I have a field called "field_categoria_del_libro_presen" that is linked to a taxonomy term. It's possible to specify more than one value. I used kint() to obtain the structure of the node. Here:
field_categoria_del_libro_presen
→array(2)
target_id
1
"1"
2
"4"
I attached the screenshot too.
screenshot
How can I get the value "1" and "4" that represent the terms of my taxonomy?
Thank you so much in advance.
Regards.
Valentina
ANOTHER QUESTION UNSOLVED:
how can I get the length of the array?
I found a solution. So I answer my question.
{{ node.field_categoria_del_libro_presen.0.target_id}}
{{ node.field_categoria_del_libro_presen.1.target_id}}
According to your self provided answer I guess you're talking about how to get the value in a twig template. Well generally speaking if you want to get the length of an array you can use the "length" filter. It looks like this:
{{ someArray|length }}
And regarding your original question, if the number of selected values varies and you want to display them all I would suggest using a for loop, it looks like this:
{% for arrayElement in someArray %}
{{ arrayElement.someKey }}
{% endfor %}
For more information look here: http://twig.sensiolabs.org/doc/tags/for.html
and here:
http://twig.sensiolabs.org/doc/filters/length.html
Let's say I have a simple product_orders collection in Meteor (_id, user_id, user_name, product_name, price_unit, quantity) and I want to show all orders of a single user in table where each row should contain:
user_name, product_name, quantity, price_unit, quantity, price_total (price_unit * quantity)
Additionally I would like to display a grand total for all user's orders.
I don't see easy way to do this in Handlebars.js template as Handlebars doesn't appear to support simple math operations. I can easily return product_order's cursor to my template but don't see easy way to calculate price_total and grand total in template.
I am thinking of creating a template helper of some sort but not sure if that's the proper direction to go. This problem looks like it must have a simple & elegant solution.
Yes, you should write a helper. Handlebars doesn't support using logic in the templates (which is a good practice as it forces you to apply the separation of concerns pattern).
A template helper looks like this:
Handlebars.registerHelper("grandTotal", function(user_name) {
var grandTotal = some_magic_to_calc_total(user_name);
return grandTotal;
});
Then you can call the helper like this from your template:
<template name="foo">
{{grandTotal user_name}}
</template>
You can read more about helpers in the Handlebars.js docs.