So I have this loop
{% if employees %}
{% set count = 1 %}
{% set multiplier = 1 %}
{% for employeeIndex, employee in employees %}
<tr>
<td>{{ employee.werknemers }}</td>
{% for workdayIndex, workday in workdays %}
{% if (employeeIndex * multiplier) == workdayIndex %}
<td>x</td>
{% if count == (employees|length) %}
{% set multiplier = multiplier * employees|length %}
{% set count = count + 1 %}
{% endif %}
{% else %}
<td></td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
{% endif %}
The result is this :
What I want too happen is for the loop too repeat it self after every employee has a date assigned to them. As u can see the first X begins at 3 and goes down too 13. What I want is the X to begin from 14 again and go down the same way. This should be possible in Twig I believe. Thanks for helping out!!
I did not test this because I can't right now test twig but I think this should work
{% if employees %}
{% set employeeCount = (employees|length) %}
{% for employeeIndex, employee in employees %}
<tr>
<td>{{ employee.werknemers }}</td>
{% for workdayIndex, workday in workdays %}
{% if (workdayIndex - employeeIndex) is divisible by (employeeCount) %}
<td>x</td>
{% else %}
<td></td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
{% endif %}
Related
I am very new to twig and have a problem which I don't know how to solve.
So I have something like this:
{% for entry in entries %}
{% set startDate = entry.begin|date_time %}
% set endDate = entry.end|date_time %}
<tr class="{{ cycle(['odd', 'even'], loop.index0) }}">
<td class="text-nowrap">
{% block date_begin %}{{ entry.begin|date_time }}{% endblock %}
{% if entry.end %}
{% block date_end %}{{ endDatum }}{% endblock %}
{% endif %}
</td>
{% endfor %}
If the entries have the same date I want the <tr> to get the same class, but how do I check if the startDate of Array.1 is the same of Array.2?
As I am not really experienced with twig I can't write much here.
You can't use the function cycle for this use-case.
As you want to keep track of the start_date of a previous entry, you will need to store the date in a (temporary) variable and use that variable to toggle the class when they are not the same date.
{% set is_even = true %}
{% set current_date = null %}
<table>
{% for entry in entries %}
{# store the first start_date in current_date so we can actually compare dates #}
{% if current_date is null %}
{% set current_date = entry.start_date %}
{% endif %}
{# toggle the class when the dates aren't the same #}
{% if current_date|date('U') != entry.start_date|date('U') %}
{% set is_even = not is_even %}
{% set current_date = entry.start_date %}
{% endif %}
<tr class="{{ is_even ? 'even':'odd' }}">
<td class="text-nowrap">
{{ entry.start_date }}
</td>
</tr>
{% endfor %}
</table>
demo
I'm trying to access the row content type to write an IF condition.
please find below the view twig file.
{% for row in rows %}
{{ row.content }}
{% endfor %}
I try the following but not working.
{% for key, row in rows %}
{% if row._entity.gettype == 'warnings'%}
{{row.content}}
{% else %}
Nothing
{% endif %}
{{kint(row)}}
{% endfor %}
You can use keys this way to iterate through the rows.
{% for key in rows|keys %}
{{ rows[key].content }}
{% endfor %}
Hope this helps ;)
I have this loop displaying six links from left to right. I would like to add a break after the fourth link but I'm new at Twig and I don't know how to add it in. Do I need another loop inside the For loop?
{% if contactLinks|length <= 6 %}
{% for link in contactLinks %}
{{ link|raw }}
{% endfor %}
{% elseif contactLinks|length >= 6 %}
{% for link in contactLinks %}
{{ link|raw }}
{% endfor %}
{% endif %}
Twig has a special loop variable that you'll need to use. See here:
http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable
{% for link in contactLinks %}
{{ link|raw }}
{% if loop.index == 4 %}
<br/>
{% endif %}
{% endfor %}
http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable
I have a menu for my site that I only want to be able to display links based upon a users permissions.
Menu example:
<ul>
<li>Dashboard</li>
<li>Products</li>
<li>Suppliers</li>
</ul>
In Twig I am currently having to repeat code to accomplish what I am needing done. I DONT like doing this!
I first check to see if the user is an admin, if they are then then automatically get access to all the links...
{% set is_admin = false %}
{% if app.security.token.user.roles is iterable %}
{% for role in app.security.token.user.roles %}
{% if role == 'ROLE_ADMIN' or role == 'ROLE_SUPER_ADMIN' %}
{% set is_admin = true %}
{% endif %}
{% endfor %}
{% endif %}
This is my menu set up currently. I need to be able to authenticate whether the link can show up based upon the users roles and permissions. This is just mock up link, but in reality I could have upwards of 50 links, so as you can see this is just not a correct way to do this.
Any suggestions would be VERY welcome on how to perhaps create a function in Twig (not aware of any way to do this) or any other possible way recommended. I just need to have a reusable function.
<ul>
{% set is_dashboard = false %}
{% for role in app.user.roles %}
{% for roleAuth in role.appRoleAuthorizations %}
{% for roleAuthRoute in roleAuth.appRoleAuthorizationRoute %}
{% if roleAuthRoute.name == "dashboard" or is_admin %}
{% set is_dashboard = true %}
{% endif %}
{% if not loop.last %}{% endif %}
{% endfor %}
{% if not loop.last %}{% endif %}
{% endfor %}
{% if not loop.last %}{% endif %}
{% endfor %}
{% if is_dashboard == true %}
<li>Dashboard</li>
{% endif %}
{% set is_products = false %}
{% for role in app.user.roles %}
{% for roleAuth in role.appRoleAuthorizations %}
{% for roleAuthRoute in roleAuth.appRoleAuthorizationRoute %}
{% if roleAuthRoute.name == "product" or is_admin %}
{% set is_products = true %}
{% endif %}
{% if not loop.last %}{% endif %}
{% endfor %}
{% if not loop.last %}{% endif %}
{% endfor %}
{% if not loop.last %}{% endif %}
{% endfor %}
{% if is_products == true %}
<li>Products</li>
{% endif %}
{% set is_suppliers = false %}
{% for role in app.user.roles %}
{% for roleAuth in role.appRoleAuthorizations %}
{% for roleAuthRoute in roleAuth.appRoleAuthorizationRoute %}
{% if roleAuthRoute.name == "supplier" or is_admin %}
{% set is_suppliers = true %}
{% endif %}
{% if not loop.last %}{% endif %}
{% endfor %}
{% if not loop.last %}{% endif %}
{% endfor %}
{% if not loop.last %}{% endif %}
{% endfor %}
{% if is_suppliers == true %}
<li>Suppliers</li>
{% endif %}
</ul>
Why don't you use Symfony's built-in {% is_granted() %} Twig function? Here's the doc. Using it should make your templates a bit more clean.
As for building menus, there's no need to do this in your templates, as this would break the single responsibility principle.
First, you should consider using KnpMenuBundle, as it allows you to dynamically build menus that could depend on basically any parameter that's accessible through the Service Container. The menu itself is usually built with EventListeners, which gives you a lot of flexibility.
If adding a bundle is not an option for you, then why don't you extract the checks you make in your template into a service itself? It could have access to virtually any value in your project. You could just pass the current context and user there, and get an array or object of booleans that can be then used in your template.
I have this code on my related products:
<h3>HAVE YOU TRIED...</h3>
{% assign max = 10 %}
{% assign count = '' %}
{% assign list = '' %}
{% capture list %},{{ product.id }}{% endcapture %}
{% for collection in product.collections %}
{% if collection.handle contains 'related' %}
{% for product_related in collection.products %}
{% capture id %},{{ product_related.id }}{% endcapture %}
{% unless list contains id %}
{% if count.size < max and product_related.images.size > 0 %}
<div class="rel-product">
<div class="rel-img">
{% for image in product_related.images offset:1 limit:1 %}
<img src="{{ image.src | product_img_url: 'compact' }}}" alt="" />
{% endfor %}
</div>
<div class="rel-cnt">
<h6>{{ product_related.title }}</h6>
<p>{{ variant.option1 }}</p>
<span class="price">{{ product_related.price | money }}</span>
</div>
</div>
{% capture count %}{{ count }}.{% endcapture %}
{% capture list %}{{ list }}{{ id }}{% endcapture %}
{% endif %}
{% endunless %}
{% endfor %}
{% endif %}
{% endfor %}
It seems to work fine for products in one collection but not at all for products in another collection, the "have you tried" box is empty. Can anyone tell me what this code is doing in terms of picking related products in shopify and why it might not be showing any related products for items in that collection.
Firstly print your 'product' array and check what it will return to you while checking for other one.