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 ;)
Related
I am looping through an array and would like to add a class if it is the first item.
I have tried
{% if field|first %}
{% if field.first %}
{% if field.first() %}
{% if loop|first %}
{% if loop.first %}
{% if loop.first() %}
none work, please help. (the reason I don't use css pseudo first is that this is a grid layout and I have am doing responsive layout things with it)
{% for field in row.fields %}
<div class="c-product-table__item {% if field|first %}{{ first_item }}{% endif %} ">
<span class="c-product-table__text">{{ field.text }}</span>
</div>
{% endfor %}
I don't know why twig filters are not working, I solved it another way. I set a var and made it blank after first loop
{% set thiscount = 'first_item' %}
{% for field in row.fields %}
<div class="c-product-table__item {{ thiscount }}">
<span class="c-product-table__text">{{ field.text }}</span>
</div>
{% set thiscount = '' %}
{% endfor %}
I have an editor with a set of buttons, and I want to display only a set of buttons based on twig::render variables.
If I include all I want it to display are buttons available, if I include individual button keys I want to display only that ones.
echo TwigLoader::render('#ui/editor.html.twig'['toolbar'=>['all']]);
echo TwigLoader::render('#ui/editor.html.twig'['toolbar'=>['font','size']]);
For the template I'm using the following code:
{% set toolbar_tools = [
{'font':'<select class="ql-font"></select>'},
{'size':'<select class="ql-size"></select>'}]
%}
<div id="button-container">
<span class="ql-formats">
{% for tool, key in toolbar_tools %}
{{ tool.key|raw}}
{% endfor %}
</span>
</div>
I'm getting an empty container.
Is this a good strategy or there are better ways?
Seems you`re looking for something like this:
{% set toolbar_tools = {
'font':'<select class="ql-font"></select>',
'size':'<select class="ql-size"></select>'
}
%}
<div id="button-container">
<span class="ql-formats">
{% if toolbar|length > 0 %}
{% for t in toolbar %}
{% if t == 'all' %}
{# show all options #}
{% for tool in toolbar_tools %}
{{ tool|raw }}
{% endfor %}
{% else %}
{# show defined options #}
{{ attribute(toolbar_tools, t)|raw }}
{% endif %}
<br />
{% endfor %}
{% endif %}
</span>
</div>
Hope you will be fine with that.
I want to use break in the Twig template engine.
{% for key, db_staff_language in db_staff_languages %}
{% for staff_language in model_data.staff_languages %}
{% if staff_language.id == db_staff_language.id %}
<option value="{{db_staff_language.id}}" selected="selected">{{db_staff_language.staff_languages_data_translation[0].value}}</option>
{% else %}
<option value="{{db_staff_language.id}}">{{db_staff_language.staff_languages_data_translation[0].value}}</option>
{% endif %}
{% break %} {# <-- Not working #}
{% endfor %}
{% endfor %}
As I couldn't solve the problem with Twig, I also tried other things, like:
-----------------------------------------------
{% autoescape true %}
<?php echo 'test' ?>
{% endautoescape %}
-----------------------------------------------
{% php %}
<?php echo 'test' ?>
{% endphp %}
-----------------------------------------------
{% verbatim %}
<?php echo "test"; ?>
{% endverbatim %}
-----------------------------------------------
{{ raw }}
<?php echo "test"; ?>
{{ endraw }}
-----------------------------------------------
Is there a solution for this problem?
Twig doesn't include a break statement. According to the official manual, the equivalent instruction is the for + if condition.
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.