I'm struggeling to select a previous for loop in Twig. I'm trying to display a few category titles in a category if that category has subcategories. If the category has no subcategories then it shhould display the titles from the previous loop. Normally this wouldn't be a problem if any category has the same depth. Unfortunatly the categories have different depths.
So what I try to do is create some sort of function that does this for me.
So for example:
Category A -> Category A.sub -> Category A.subsub
Title1 Title1.1 Title1.2
Title1 Title1.1 Title1.2
Category B -> Category B.sub -> Category A.subsub
Title1 Title1.1 Title1.1
Title1 Title1.1 Title1.1
As you can see Category B.sub.sub hasn't any subcategories. If that's the case it should show the subcategories from Category B.sub. Normally I would do something like this:
{% for category in shop.categories %}
{{ category.title }}
{% if category.subs %}
{% for category in category.subs %}
{{ category.title }}
{% if category.subs %}
{% for category in category.subs %}
{{ category.title }}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
Is there any way to create somesort of function that checks if a category has subcategories. If that's not the case the access the previous loop and display those category names.
I thought this was as simple as:
{% elseif not category.subs %}
{# Do this #}
But that's not the case :(
My suggestion is to make the structure of the arrays alike within your php code and don't put such kind of logic in the template.
So in php you wold have something like this:
if (!isset($categoryB['sub']['subsub']) {
$categoryB['sub']['subsub'] = $categoryA['sub']['subsub'];
}
and then your template you just iterate:
{% for category in shop.categories %}
{{ category.title }}
{% for category in category.subs %}
{{ category.title }}
{% for category in category.subs.subsub %}
{{ category.title }}
And I would also suggest to make it recursive so you would have something like:
{% itarerateCategoryes categories %}
Agreeing with Fyntasia I wouldn't have lots of logic in the template, I would parse the data in the controller to the form I wanted.
However assuming your data array is something like (couldn't understand your notation);
$categories = [
0 => [
'top' => ['Atop1', 'Atop2'],
'middle' => ['Amiddle1', 'Amiddle2'],
'bottom' => ['Abottom1', 'Abottom2'],
],
1 => [
'top' => ['Btop1', 'Btop2'],
'middle' => ['Bmiddle1', 'Bmiddle2'],
],
];
Something like;
{% for main_index, category in categories %}
{% if category.top is defined and category.top|length > 0 %}
{{ loop.index0 }} has top values
{% endif %}
{% if category.middle is defined and category.middle|length > 0 %}
{{ loop.index0 }} has middle values
{% endif %}
{% if category.bottom is defined and category.bottom|length > 0 %}
{{ loop.index0 }} has bottom values
{% else %}
{{ loop.index0 }} has no value so using {{ categories[loop.index0 - 1].bottom|join(', ') }}
{% endif %}
<br />
{% endfor %}
Outputs something like;
0 has top values 0 has middle values 0 has bottom values
1 has top values 1 has middle values 1 has no value so using Abottom1, Abottom2
Related
Twig newbie here :) And I'm using WordPress.
In a .twig file, I can display the category.slug this way:
{% for category in post.terms('category') %}
{{ category.slug }}
{% endfor %}
But how do I use an if construct with the category.slug?
I.e., I want to display the category if the post is in the category in-the-news. This obviously doesn't work:
{% for category in post.terms('category') %}
{% if category.slug == in-the-news %}
In The News
{% else %}
Other Category
{% endif %}
{% endfor %}
This is twig version is 1.34. Fiddle here from Matteo: https://twigfiddle.com/89t9gr
This works. I think the issue in my localhost and Twig v1.34 was having no quotes around the category slugs. And, for me, {% for category in categories %} does not work and does not throw an error.
{% for category in post.terms('category') %}
{% if category.slug == "in-the-news" %}
In The News
{% elseif category.slug == "videos" %}
Video
{% else %}
All Else
{% endif %}
{% endfor %}
Setting up a Drupal 8 custom view for Top 4 items, I want a different layout for item 1 than the remaining. I have override files for the custom view, but for this example I'm using base files to keep it simple.
In views-view.html.twig base file we have:
<div class="view-content">
{{ rows }}
</div>
In node.html.twig base file we have:
<div {{ content_attributes.addClass('content') }}>
{{ content }}
</div>
In node.html.twig I am aiming for something like:
{% if row_counter = 1 %}
output this markup / fields
{% else %}
do something boring with the other 3 items.
{% endif %}
I was able to set row_counter in the views-view.twig.html file:
{% for row in rows %}
{% set row_counter = loop.index %}
<div{{ row.attributes }}>
{{ row_counter }}
</div>
{% endfor %}
But I need to check against the value of {{ row_counter }} in the node.html.twig file....
What other properties are available in node.html.twig to check against its position in the list?
From the documentation
The loop variable
Inside of a for loop block you can access some special variables:
Variable Description
-----------------------------------------------------------------
loop.index The current iteration of the loop. (1 indexed)
loop.index0 The current iteration of the loop. (0 indexed)
loop.revindex The number of iterations from the end of the loop (1 indexed)
loop.revindex0 The number of iterations from the end of the loop (0 indexed)
loop.first True if first iteration
loop.last True if last iteration
loop.length The number of items in the sequence
loop.parent The parent context
edit: every variable know in the parent template is also known inside an include as context is passed by default. Only macro's don't know the parent's context
controller
<?php
require __DIR__ . '/../requires/propel_standalone.php';
echo $twig->render('tests/items.html', [
'items' => [
'Abc',
'def',
'ghi',
'jkl',
'mno',
'pqr',
'stu',
'vwx',
'z',
],
]);
items.twig
<!doctype>
<html>
<head><title>Test</title></head>
<body>
{% for item in items %}
{% include "tests/item.html" %}
{% endfor %}
</body>
</html>
item.twig
{% set order = 'Nothing to report' %}
{% if loop.first %}
{% set order = 'I\'m first' %}
{% endif %}
{% if loop.last %}
{% set order = 'I\'m last' %}
{% endif %}
{% if loop.index is even %}
{% set order = 'I\'m even' %}
{% endif %}
{% if loop.index is divisible by(5) %}
{% set order = 'I can be dived by 5' %}
{% endif %}
{% if loop.index is divisible by(3) %}
{% set order = 'I can be dived by 3' %}
{% endif %}
<div>
<b>{{ loop.index }}:</b>{{ order }} - {{ item }}
</div>
You could do something like this if a class is enough:
Your views template:
{% for row in rows %}
<div class="{% if loop.index == 1 %}first_element{% endif %}">
{{ row.content }}
</div>
{% endfor %}
And then just style it appropriately.
I have two loops in Twig. One is to check the stock level of a product, the other is to display some product options. I'm trying to create a variable from one loop which I can use inside the other to add a class name to a list(s).
I can't get that to work. Any help is more then welcome...
What I have is this:
{% set stockLevel = '' %}
{% for variant in product.variants %}
{% set stockLevel = variant.stock.level %}
{{ stockLevel }} // gives 1 0 1 1 (So all sizes except the second one are available)
{% endfor %}
{% for option in product.options %}
<ul class="optionslist">
{% if option.values %}
{% for value in option.values %}
<li class=" {% if stockLevel == 0 %}not_on_stock {% endif %}" >
<a class="item">etc...</a>
</li>
{% endfor %}
{% endif %}
</ul>
{% endfor %}
I believe there is something wrong the way you are handling your data. Right now, event if your variable was accessible in your second for loop, the value of it would be the one set lastly and your script would've failed anyway.
I can suggest a bit of a hacky way, feel free to improvise or use the idea behind it.
Say, we assign an empty array:
{% set variantsArray = [] %}
Then, we will use the filter merge to fill it with some dummy data(that's where your first loop comes into play)
{% for number in 1..5 %}
{% set variantsArray = variantsArray | merge([number]) %}
{% endfor %}
Then to be sure, we can access all the values there, we will just dump it out(that's where your second loop comes)
{% for index in 1..3 %}
{{ dump(variantsArray) }}
{% endfor %}
The following dump generated:
array:5 [▼
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
]
Hope you got the idea. Any suggestions are kindly welcome.
I am not sure what you are trying to do, how do you get your stock level from the variants? i think it is better to have a service function which help you get your stock level by product, the code would be cleaner :
controller action
$stock_helper = $this->container->get('stockhelper_service');
$stocklevel = $stock_helper->getStockLevelByProduct($product); //getstocklevelbyproduct handle your logic and return the stock level integer.
pass this variable to the twig.
One variable in your twig, no loops there, clean view, understandable code in your view and action.
Ok, I found the solution. The trick is to "connect" both values. Like so:
{% for option in product.options %}
{% if option.values %}
<ul id="product_configure_option2_{{ option.id }}" data-id="{{ option.id }}">
{% for value in option.values %}
{% set stock = null %}
{% for variant in product.variants **if variant.title == (option.title ~ ': ' ~ value.title)** %}
{% set stock = variant.stock %}
{% endfor %}
<li value="{{ value.id }}"{% if not stock.available %} class="not_on_stock"{% endif %}>{{ value.title }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
First of all I'm learning Twig.
I was wondering if it is possible wit Twig to compare two different values from different arrays/lists?!
I have two list of items I call them like so:
{% if page.cart %}
{% for product in page.cart.products %}
{{ product.id }}
{% endfor %}
{% endif %}
And:
{% if products %}
{% for product in products %}
{{ product.id }}
{% endfor %}
{% endif %}
I want to compare both product.id's so I can create a new statement. Is there any way to compare both values? The idea is to check if an id is present in page.cart.products and if so then do something.
I want to create a new statement to display some info. Something like so:
{% if page.cart %}
{% for product in page.cart.products %}
{% set cartId %}{{ product.id }}{% endset %}
{% endfor %}
{% endif %}
{% if products %}
{% for product in products %}
{% set listId %}{{ product.id }}{% endset %}
{% endfor %}
{% endif %}
{% if cartId == listId %}
.... do this ....
{% endif %}
Any help greatly appreciated!
You can loop over one array and check if the id is present in the second one. If it's there, you can do something.
{# In case you want to store them, you can do so in an array #}
{% set repeatedIds = [] %}
{% for productCart in page.cart.products if page.cart %}
{% for product in products if products %}
{% if productCart.id == product.id %}
<p>This id -> {{ product.id }} is already in page.cart.products</p>
{% set repeatedIds = repeatedIds|merge([product.id]) %}
{% endif %}
{% endfor %}
{% endfor %}
{{ dump(repeatedIds) }}
It's a very basic search algorithm and the cost is quadratic. Obviously, there are more efficient ways to look for an element in an array (though more complicated to implement).
If the amount of products you have to deal with is not very big, you could use this solution. However, if you have, let's say, more than one hundred products in each array (or you feel that the algorithm is slowing down your loading time), you could do this process in the controller using more sophisticated methods and PHP and just pass the result to the template.
Hope it helps.
I have an object called "template.Highlights" where exists 7 items. All items have 2 fields: 'name','desc'. How can I access (without multiples if's) an item through the value of the 'name' field? (the names are: 'hl1', 'hl2', 'hl3', 'hl4', 'hl5', 'hl6', 'hl7').
{% for template.Highlights as highlight %}
{% if highlight.name == 'hl5' %}
{% set item = highlight %}
{% endif %}
{% endfor %}
{{ item.name }}, {{ item.desc }}