Is it possible to set a option value to a twig variable?
I want to accomplish something like this.
<select>
<option value="10">10</option>
</select>
{% set optVal = option value here %}//don't know if this is possible.
{% if optVal %}
//do something here
{% endif %}
Related
I'm trying to check if a string is empty in a twig template but I don't understand this behaviour. I want to show a textarea element when the condition is met. Here's the code:
{% if item.payload.customizationText|length == 0 %}
<textarea class="customization-text">{{ item.payload.customizationText|trim }}</textarea>
{% endif %}
It always shows the textarea, whether the string has something in it or not. Am I missing something?
It's in shopware 6 by the way and the dump() fuction throws an error because it's undefined.
Also, this expression just outputs the string. Is the length expression not available?
{{ item.payload.customizationText|length }}
I believe there is a mistake in the question. It does not make sense to print the variable, only if it is empty.
If you want to show the text area in case the customizationText has some contents, you should use this:
{% if item.payload.customizationText|trim|length %}
<textarea class="customization-text">{{ item.payload.customizationText|trim }}</textarea>
{% endif %}
https://twigfiddle.com/nmk2kq/4
You can try it.
{% if item.payload.customizationText is defined and item.payload.customizationText is empty %}
<textarea class="customization-text">{{ item.payload.customizationText|trim }}</textarea>
{% endif %}
I need to loop through a table in .twig and if two columns are equal, it should return a button to delete., for example:
loop through boxes and if the quantity of boxes is equal to the available_quantity then I can show a delete button.
{% for toy in boxes.toys %}
{% if toy.quantity == toy.available_quantity %}
Delete
{% endif %}
{% endfor %}
Right now, this returns a button for each true case. I just need one button if all are true, if on or all are false I dont want a button
What you could do is something like this, as soon as you find a row where the quantities are different, set the value to false, so it'll never display the button:
{% set delete = true %}
{% for toy in boxes.toys %}
{% if toy.quantity != toy.available_quantity %}
{% set delete = false %}
{% endif %}
{% endfor %}
{% if delete %}
Delete
{% endif %}
Unfortunately Twig hasn't got a way to then break out of the loop, so it'll continue to loop over all the entries, but that won't be a problem.
Alternatively, you can use this method which will only loop over entries that match a certain condition, to do basically the same thing:
{% set delete = true %}
{% for toy in boxes.toys if toy.quantity != toy.available_quantity %}
{% set delete = false %}
{% endfor %}
{% if delete %}
Delete
{% endif %}
In this case, it'll only loop over the entries which have the values not matching, and in that case, set delete to false.
I came across a fix, not sure how ideal or correct this is. Im simply running the check inside the anchor tag and adding a hide class if quantities don't match up
<a href="" type="button" class="btn btn-xs btn-danger
{% for toy in boxes.toys %}
{% if toy.quantity != toy.available_quantity %}
hide
{% endif %}
{% endfor %}
">Delete</a>
I am passing two objects 'editsub_obj' and 'cat' both key-value pairs to the template.
The main aim is to keep option tag selected when name from both object list matches but the code is not working.
<select class="form-control" id="" name="cat_name" required>
{% if not editsub_obj %}
{% for data in cat %}
<option value="{{data.id}}">{{data.category_name}}</option>
{% endfor %}
{% else %}
{% for data in cat %}
{% if editsub_obj.category_name == data.category_name %}
<option value="{{data.id}}" selected>{{data.category_name}}</option>
{% else %}
<option value="{{data.id}}">{{data.category_name}}</option>
{% endif %}
{% endfor %}
{% endif %}
</select>
Expected :
selected should be selected with the category_name matches in both objects list.
Actual :
none of the options are showing as selected.
Your code can reduce to ::
<select class="form-control" id="" name="cat_name" required>
{% for data in cat %}
<option value="{{data.id}}"
{% if editsub_obj and editsub_obj.category_name in data.category_name %}
selected
{%endif%}>
{{data.category_name}}
</option>
{% endfor %}
</select>
Say I have the following headline.twig partial:
<h2>{{ headline }}</h2>
and I want to include it in two places, once as:
<% for article in teasers %>
{{ include('headline.twig') }}
<% endfor %>
And then simply:
{{ include('headline.twig') }}
Is it possible to pass an include tag or function its context, so that the include "knows" that in the first instance the headline variable is actually article.headline?
I'm looking for a systematic way to do this, not with something like
{{ include('headline.twig', {headline: article.headline}) }}
If you want headline to be in the main context of your included file in all cases, you can do something like this:
{% for article in teasers %}
{% set headline = article.headline %}
{{ include('headline.twig') }}
{% endfor %}
But this will overwrite any existing headline variable in your current context if you're re-setting it this way (and risk to repeat 2 times the last iteration of teasers.article.headline).
The best solution if you want to keep your current context AND overwrite headline variable is to use the merge filter:
{% for article in teasers %}
{{ include('headline.twig', _context|merge({headline: article.headline})) }}
{% endfor %}
By default whole context is passed to included template.
So it will work in headline.twig:
<h2>{{ article.headline }}</h2>
And then in main template you only call:
{% include('headline.twig') %}
But article variable must be defined in moment of calling include.
I was wondering if it is possible to create some sort of condition which can be used on 3 different places in a template with Twig.
Let's say I have 3 slider images with a link. A user of the template has the option to choose between a button link or a normal link via a dropdown menu. Like so (below is only the first slider):
{% if theme.slide1 %}
<li class="animate-in f1">
<div class="wrap">
{{ 'Read more' | t }}
</div>
</li>
{% endif %}
{% if theme.slide2 %}
<li class="animate-in f2">
<div class="wrap">
{{ 'Read more' | t }}
</div>
</li>
{% endif %}
Normally you would do something like:
{% if theme.slide1_link_setting == 'Link' %} do this {% else %} do that {% endif %}
You have to do this 3 times if you have 3 slider images. But what if you have eg. 10 slider images?
I tried to create a function which checks if a setting is a button or normal link.
{% set link_style = theme.slide1_link in ['link', 'button'] ? 'normal-link': 'btn btn-custom-2 btn-sequence' %}
First of all the function above doesn't work.
Second I don't know how to set the number of the slide you're in. I want something like below:
{% set link_style = theme.slide[numberOfSlide]_link in ['link', 'button'] ? 'normal-link': 'btn btn-custom-2 btn-sequence' %}
Is there anybody who can help me with this?