twig looping empty result - php

i'm building a navigation menu when I loop the results from database it's working properly with this code
foreach ($menu as $item) {
echo $item->name_english . ' ';
if ($item->childs->count()) {
foreach ($item->childs as $row) {
echo $row->name_english . ' | ';
}
}
}
and this id the output
first second child1| child2 | third fourth
when I use the same code in twig I get no results from childs loop
{% if menu %}
<ul>
{% for item in menu %}
<li>{{ item.name_english }}</li>
{% if item.childs.count() %}
<ul>
{% for stuff in item.childs %}
<li>{{ stuff.name_english }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
</ul>
{% endif %}
and this is the output
first
second
third
fourth

To check the count of an array in twig you use length filter. Moreover you should be more specific when using twig. use menu is not null instead of menu.
{% if menu is not null and menu|length > 0 %}
<ul>
{% for item in menu %}
<li>{{ item.name_english }}</li>
{% if item.childs|length > 0 %}
<ul>
{% for stuff in item.childs %}
<li>{{ stuff.name_english }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
</ul>
{% endif %}

Related

Render a form ONLY ONCE inside a for loop in a twig template

I have a twig template to display audio options (Auto play and Continuos play) as shown in the screen shot:
Click to see the screen shot of the page
The following is the code in my twig file. {{ audio_options_form}} renders the form with the checkboxes.I want to display the check boxes only once if {{ item.audio }} is true. Please let me know what changes should I make:
<div id="textcontent">
{% set field = data|slice(2) %}
{% set field = field|slice(0, -1) %}
<div class="col-md-12">
<div class = "source-box audio">
{% for item in field %}
{% if item.audio %}
{{ audio_options_form }}
{% if data.play.autoplay == 1 %}
<audio autoplay controls src= "{{item.audio}}" id = "audio-play">
Your browser does not support the
<code>audio</code> element.
</audio>
{% elseif data.play.continuousplay == 1 %}
<audio autoplay controls src= "{{item.audio}}" id = "audio-play" onended="continousPlay('{{data.lastlevel}}')">
Your browser does not support the
<code>audio</code> element.
</audio>
{% else %}
<audio controls src= "{{item.audio}}" id = "audio-play">
Your browser does not support the
<code>audio</code> element.
</audio>
{% endif %}
<div id="source-controls">
{# {% if allow_edit == 1 %}
<a class="edit-text" href="{{ path('heritage_text_manager.editsource', {'sourceid': item.uniqueid}, {'query': {'destination': path('heritage_ui.contentpage', {'textid': textid})}}) }}">{{ 'Edit'|t }}</a>
{% endif %} #}
<a class="more use-ajax" data-toggle="modal" data-dialog-type="modal" href="{{ path('heritage_ui.metadata', {'sourceid': item.uniqueid}) }}">{{ 'More'|t }}</a>
</div>
{% endif %}
{% endfor %}
</div>
Either create a 2nd loop with a flag or use the filter filter.
{% set show_audio_form = false %}
{% for item in field %}
{% if item.audio %}
{% set show_audio_form = true %}
{% endif %}
{% endfor %}
{% if show_audio_form %}{{ audio_options_form }}{% endif %}
{% for item in field %}
... display audio ...
{% endfor %}
{% if items|filter(v => v.audio|default)| length %}
{{ audio_options_form }}
{% endif %}
{% for item in items %}
... display audio ...
{% endfor %}
demo

How to add a BR tag after fourth loop using Twig

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

twig template display articles from specific date

I am making a site using twig php template.
I want to display the articles published in November.
So far I have this:
{% if date(article.published) == date('november') %}
{% for article in articles %}
<li> {{ article.title }} </li>
{% endfor %}
{% endif %}
(Thanks in advance)
You should use IF clause inside of the loop, not outside of it:
{% for article in articles %}
{% if date(article.published) == date('november') %}
<li> {{ article.title }} </li>
{% endif %}
{% endfor %}
You are looking for:
{%
if article.published|date('U') > '2014-11-01'|date('U')
and article.published|date('U') < '2014-12-01'|date('U')
%}
The date('U') filter will convert dates to the timestamp format (number of secs since 1970-01-01), so we can do some dates comparisons from here and filter all dates between 2014-11-01 and 2014-11-31 23:59:59.
And as #Giedrius pointed out, you should put the if inside the loop.
This becomes:
{% for article in articles %}
{%
if article.published|date('U') > '2014-11-01'|date('U')
and article.published|date('U') < '2014-12-01'|date('U')
%}
<li> {{ article.title }} </li>
{% endif %}
{% endfor %}

Create a navigation menu using Twig

How do you create a navigation menu using Twig? Note that I am not using Sympony2.
Given the following array passed to a Twig template:
$menu_main=array(
'active'=>2,
'menu'=>array(
array('components_id'=>2,'name'=>'First Link','newwindow'=>0),
array('components_id'=>3,'name'=>'Second Link','newwindow'=>0),
array('components_id'=>7,'name'=>'Third Link','newwindow'=>1),
array('components_id'=>8,'name'=>'Forth Link','newwindow'=>0)
)
);
I am trying to create the following navigation menu:
<ul>
<li class="first active">First Link</li>
<li>Second Link</li>
<li>Third Link</li>
<li class="last">Forth Link</li>
</ul>
Below is my non-working attempt:
{#
menu_main is an associated array containing active key and menu key.
menu is an associated array of containing name, components_id, and whether it should open a new window
#}
<ul>
{% for item in menu_main.menu %}
<li
{% if item.components_id == menu_main.active %}
class="active"
{# How do I add first and last class? Maybe using length? #}
{% endif%}>
{{item[1]}}{% if item.newwindow %} target="_blank" {% endif%}>
</li>
{% endfor %}
</ul>
This should work for you.
As $main_menu['menu'] array is associative twig won't understand item[0] or item[1]
{#
menu_main is an associated array containing active key and menu key.
menu is an associated array of containing name, components_id, and whether it should open a new window
#}
<ul>
{% for item in menu_main.menu %}
{% set classes = "" %}
<li
{% if item.components_id == menu_main.active %}
{% set classes = classes~' active ' %}
{% endif%}>
{% if loop.first %}
{% set classes = classes~' first ' %}
{% elseif loop.last %}
{% set classes = classes~' last ' %}
{% endif %}
<a class="{{ classes }}" href="index.php?cid={{ item['components_id'] }}">{{item['name']}}</a>
</li>
{% endfor %}
</ul>

Shopify don't show certain items in menu

This is the code for my header menu:
<ul>
{% for link in linklists.main-menu.links %}
<li class="{% if forloop.first %}first{% elsif forloop.last %}last{%endif%}">
{% assign child_list_handle = link.title | handleize %}
{% if linklists[child_list_handle].links != blank %}
<div class="has-dropdown">
<a href="{{ link.url }}" class="clearfix">
<span class="nav-label">{{ link.title | escape }}</span>
<span class="nav-arrow"></span>
</a>
<ul>
{% for childlink in linklists[child_list_handle].links %}
<li>{{ childlink.title | escape }}</li>
{% endfor %}
</ul>
</div>
{% else %}
{{ link.title | escape }}
{% endif %}
</li>
{% endfor %}
</ul>
I am not sure what lanugage this is written in but I want to change it so that any item named "featrued" is not shown in the menu. The sudo code would be something like this:
if name!="featured" then { ... }...
This is what I tried:
{% if link.title != featured %}
...
{% endif %}
Could anyone please help me understand what adjustment would need to be made?
You're close! You just need to enclose the link's title in quotation marks.
Try this:
<ul>
{% for link in linklists.main-menu.links %}
{% if link.title != 'featured' %}
<li class="{% if forloop.first %}first{% elsif forloop.last %}last{%endif%}">
...
</li>
{% endif %}
{% endfor %}
</ul>

Categories