I am a beginner in symfony2 .
I have a problem in a loop with a passed parameter to twig tepmlate: (show nbr stars )
{% for i in 0..4 %}
<span class="glyphicon glyphicon-star"></span>
{% endfor %}
The number of iteration (nbr) is passed as a parameter, I have tested this , but not working.
{% for i in 0..{{nbr}} %}
<span class="glyphicon glyphicon-star"></span>
{% endfor %}
Say you want to iterate with a given parameter
return $this->render('AcmeFoo::foo.html.twig', array(
'number' => 42
));
Your TWIG template should look like
{% for i in 0..number %}
{{ i }}
{% endfor %}
This also works for runtime-set variables
{% set number = 5 %}
{% for i in 0..number %}
{{ i }}
{% endfor %}
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 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 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.
I have the following code in my controller:
$this->get('session')->getFlashBag()->add(
'storeinfo',
'hayooo'
);
return $this->redirect($this->generateUrl('AppMainBundle_item_detailed_view', array('id' => $picture->getId(), 'caption' => $picture->getURLCaption())), 301);
and my twig looks like this:
{% if app.session.flashbag.get('storeinfo') %}
<div class="comment-confirmation">
{% for flashMessage in app.session.flashbag.get('storeinfo') %}
<p> <b> anjing banget </b></p>
{% endfor %}
</div>
</div>
{% else %}
<p> Oopsie </p>
{% endif %}
so it goes to the first if block however inside the forloop, there is no flashMessage. Why is this?
The FOSUserBundle does something like this, you could also have a look at this great imlementation of a FlashListener
{% for type, messages in app.session.flashbag.all() %}
{% for message in messages %}
{# Will print all your messages #}
{{ message }}
{# Will do something in particular for storeinfo if you want to #}
{% if type is sameas('storeinfo') %}
<p> <b> anjing banget </b></p>
{% endif %}
{% endfor %}
{% endfor %}