This question already has answers here:
Include Twig with multiple parameters
(2 answers)
Closed 4 years ago.
How to use dynamic variables in the twig include?
I want to use an include where I pass some variables, and the value of those variables changes as the read comes from the database
$product = $twig->load('product.twig');
And inside the product.twig, you have {{product_name}}
But in my home.twig file, where the include is, there will be several for with different database read
bringing different results, so how do you make it dynamic?
For me not having to create multiple .twig files on each read from the database
In my variable $products I have all the data to feed the variables that are in the product.twig
However, I will have several variables such as:
$products_demo
$products_promo
etc
home.twig:
{% include product %}
in your case
$products_demo
$products_promo
I think you can use like at your product.twig
{% if product.group == "demo" %}
{# your code for $products_demo here #}
{% elseif product.group == "promo" %}
{# your code for $products_promo here #}
{% else %}
{# another code #}
{% endif %}
product.group is array for your product
product[0][group] = demo;
product[1][group] = promo;
etc
Related
So i am working on a Shopware shop, and i want to read a MediaEntity in Twig. To do so, i am creating a string with the node path (adding the product ID as a variable), which just works fine.
To actually access the MediaEntity, i need to convert this string into a real node path. How do i do that? Or is there maybe another way to create this path?
Here's my code:
{% block component_product_box %}
{{ parent() }}
{% set coverIds = "context.extensions.#{product.coverId}.elements" %}
{{ dump() }}
{% endblock %}
I tried it roughly and something like this should work:
{% set coverIds = _context['extensions'][product.coverId]['elements'] %}
This should solve your problem, I hope.
If you really need to work with a string and "dots" notation, this could be of help:
How to use Twig's attributed function to access nested object properties
I would like to define a twig object that contains two properties :
The first one is a short text
The second one is a large text (so, i need to use {% set %}{% endset %} to keep readability of the template)
I first tried like this :
{% block principal %}
{% set a_form_help = {'help_toggle_label': 'See the big form field tip', 'help_content': ''} %}
{% set a_form_help.help_content %}>
<h1>This is rules for the field</h1>
<p>A looonng text that will be contained in a collapsed block</p>
<p>And several lines of tips for this field</p>
{% endset %}
{% endblock %}
But Twig complains about the use of the dot in the second {% set %} tag (for setting the large text content in the help_content property).
I've found this message (Twig - Why does it not allow us to set object / array values?) and then done this workaround by using a temporary variable:
{% block principal %}
{% set tmp_help_content %}>
<h1>This is rules for the field</h1>
<p>A looonng text that will be contained in a collapsed block</p>
{% endset %}
{% set a_form_help = {'help_toggle_label': 'See the big form field tip', 'help_content': tmp_help_content} %}
{{ dump(a_form_help) }}
{% endblock %}
I've used a a temporary variable since using the merge() filter would give the same result.
Is this workaroud a good practice or is there a better/cleaner way ?
Thanks in advance for any tip|advice ! :-)
There is no clean way to do it in twig.
Is this workaroud a good practice or is there a better/cleaner way ?
It is not a good practice. As twig is a templating language its responsability is only to display data, not to structure it. Your View Object should have been created elsewhere
Explanation:
I pull these values from my local database and try to display them on the front-end. The issue is, that I have 2 languages that I need to cater to.
Example:
{% if activeLocale == "si" %}
{{ record.estate_type_SI|raw }}
{% elseif activeLocale == "en" %}
{{ record.estate_type_EN|raw }}
{% endif %}
This works, but when I have multiple items it gets gruesome because I have to write everything down two times. What this does is that depending on the language a value from a different column in the database is pulled.
I am wondering if I can do something similar to this:
{{ record.estate_type_{{"SI"|trans}}|raw }}
I will gladly buy you a beer if you can help me out with this.
Cheers!
EDIT: Variables
Using attribute , you can access a property of an object in a dynamic way. Then you just have to use upper filter to match what you need.
{{ attribute(record, 'estate_type_'~ activeLocale|upper)|raw }}
I wonder if there is a way of creating multi statements in TWIG
Example: two separate statements ...
{% set foo:bar %}
{% set baz:qux %}
into one single statement
{%
set foo:bar
set baz:qux
%}
No you can't. set is a "tag", all thing after are compiled with the Token Parser for the "tag".
I just can't get this to work:
It should set var_2 based on the URL query string value of var_1
The problem is where I call var_1 with {{var_1}}
I've tried various other methods but all throw different errors.
// var_3 set elsewhere
{% set var_1 %}test-{{var_3}}{% endset %}
{% set var_2 = app.request.get({{var_1}}) %}
// need var_2 set for rest of script
You can't use another tag ({{ ... }}) inside a twig tag ({% ... %}). So this is not going to work:
{% set var_2 = app.request.get({{var_1}}) %}
A solution is to just put the variable in function argument:
{% set var_2 = app.request.get(var_1) %}
You don't need to (and often cannot) use {{ }} within twig logic. {{ }} is used to output something to the response. To use a variable in line just name the variable. Also remember that ~ will join strings, but some people don't like using it for some reason!
{% set var_1 = 'test-' ~ var_3 %}
{% set var_2 = app.request.get(var_1) %}