Shopify don't show certain items in menu - php

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>

Related

get first item in loop

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 %}

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

Symfony - How can I omit the first item in a twig template's loop?

I want to display news without first one. How I can achieve this?
Here is code I have to change:
<div class="home-box-news carousel-news slide home-box-shadow" id="news" style="clear: both;">
<ol class="carousel-indicators news-box">
{% for i in 0..news|length-1 %}
{% if loop.index is not divisibleby (2) %}
<li data-target="#news" data-slide-to="{{ i / 2 }}" {% if loop.first %}class="active"{% endif %}></li>
{% endif %}
{% endfor %}
</ol>
<div class="carousel-inner">
{% for item in news %}
{% if loop.index is not divisibleby (2) %}
<div class="item{% if loop.first %} active{% endif %}">
<div class="carousel-caption">
{% endif %}
<h3>{{ item.name }}</h3>
<p class="date">{{ item.createdAt|date('d.m.Y, G:i') }}</p>
<p {% if loop.index is divisibleby (2) %}style="border-bottom: 0;" {% endif %}>{{ item.content[:110]|nl2br }}{% if item.content|length > 110 %}... <a class="more" href="{{ path('home_news_index') }}">czytaj dalej</a>{% endif %}</p>
{% if loop.index is divisibleby (2) or loop.last %}
</div>
</div>
{% endif %}
{% endfor %}
</div>
</div>
There is more concise version of what Robert suggested:
{% for item in news[1:] %}
You can use slice filter which works like array_slice() function in PHP.
{% for item in news[1, news.length -1 ] %}
put this in your loop to omit first news
{% if loop.index0 > 0 %}
{# display your news #}
{% endif %}
Of course there is another answer which may/may not be cleaner/simpler/w/e - don't pass the row to Twig in the first place.

twig looping empty result

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 %}

Shopify - Insert image between list of items

I am trying to list a load of products, and insert an image between the second last and the last item. At the moment I have this code on my index:
<ul class="clearfix">
{% for product in collections.drinks.products %}
{% include 'snippet-product-item' with 'four' %}
{% endfor %}
</ul>
This in my product-item:
{% if snippet-product-item == '' %}{% assign snippet-product-item = 'four' %}{% endif %}
<li class="{{ snippet-product-item }}-per-row clearfix">
<div class="coll-image-wrap{% unless product.available %} sold-out{% endunless %}">
{% if product.available == false %}
{% assign ratio = settings.product_img_w_to_h_ratio | times: 1.0 %}
<a href="{{ product.url | within: collection }}">
<img src="{{ product.featured_image.src | product_img_url: 'large' }}" alt="{{ product.featured_image.alt | escape }}" />
</a>
</div><!-- .coll-image-wrap -->
</li>
So I'm guessing I need to put some code in my index block to work out how many items there are in the collection, and then for the last one add an extra image in before it. Can anyone help me figure out how that might be done please?
This is my useless attempt:
{% assign num = 0 %}
{% for product in collections.drinks.products %}
//Do something like this to work out number of items in collection
{% assign num = temp %}
// Then minus one and insert the image so it appears between second to last and last items
{% endfor %}
I would try something like this:
{% for product in collections.drinks.products %}
{% if forloop.last %}
// Insert image...
{% endif %}
{% include 'snippet-product-item' with 'four' %}
{% endfor %}

Categories