Shopify / Liquid: Separate a group of tags, then placing them on a select component - collections

I have a large set of tags, and I want to be able to sepparate them. For example, I have the Green, Blue, Red, Black, Colorless and White tags (as well as some other tags that aren't colors, so I won't put them here) and I want to place them in an array.
I tried with something like this:
{% assign colors = "" | split: '' %}
{% for tag in collection.all_tags %}
{% if tag == 'Black' or tag == 'Blue' or tag == 'Colorless' or tag == 'Red' or tag == 'White' %}
{% assign comma = "," %}
{% assign color = tag | split: '_' %}
{% assign colors = colors | concat: color %}
{% assign colors = colors | concat: comma %}
{% endif %}
{% endfor %}
However, that returns me BlackBlueGreenRedColorlessWhite, without any spaces whatsoever. Also, is there a way I can place those selected tags in a <select> component so I can filter my products by the selected tag? Like in the example provided in this page but using a <select> instead of a <ul>.

first, categorizes your tag by having a label;
i.e: color-blue, color-red, color-green, etc.
why? You will not need to add more if-else statement;
then, with code below, you will get an array of color;
{% assign color = "" %}
{% for tag in collections.all_tags %}
{% if tag contains 'color-' %}
{% assign trimmed = tag | split: "-" | last %}
{% assign color = color | append: trimmed | append: ", " %}
{% endif %}
{% endfor %}
{% assign color = color | split: ", " %}
<select name="color" id="color">
{% for x in color %}
<option value="{{x}}">{{x}}</option>
{% endfor %}
</select>

Related

Rendering Product in Shopify Blog Post Article using card-product.liquid

I'm trying to show products card inside blog posts / articles of shopify.
I've edited main-article.liquid so that when the content of an article is rendered, it checks the content for a particular string and then splits the code.
if a split has occurred than it isolates the handle of the product.
then it will need to render the product based on the product handle as a card-product.
card-product is the element in which a product is show in a collection ( as in a catalog list )
though I cannot render the product but just a dummy element as in this
I've followed 2 tutorials to get this far:
happypoint
thefunction
the first one is using a snippet that is not present in my theme ( dawn ) but also adding it just to se some results brings out same missing content problem but in a different design.
{%- when 'content'-%}
<div class="article-template__content page-width page-width--narrow rte" itemprop="articleBody" {{ block.shopify_attributes }}>
{% assign divider = '[PROD]' %}
{% assign dividerClose = '[/PROD]' %}
{% assign text = article.content | split: divider %}
{{ text[0] }}
{% for divider in text offset:1 %}
{% assign x = forloop.index %}
{% assign newtext = text[x] | split: dividerClose %}
{% assign productHandle = newtext[0] %}
{% assign product = all_products[productHandle] %}
<div class="product_item">
{% render 'card-product',
media_aspect_ratio: section.settings.image_ratio,
show_secondary_image: section.settings.show_secondary_image,
show_vendor: section.settings.show_vendor,
show_rating: section.settings.show_rating,
lazy_load: lazy_load,
show_quick_add: section.settings.enable_quick_add,
section_id: section.id
%}
</div>
{{ newtext[1] }}
{% endfor %}
</div>
How can I render the product as a card-product.liquid?

Twig - Get corresponding alphabet letter in loop

I want to use the loop.index variable in twig to get the corresponding alphabet letter (1 = A, 2 = B, etc).
{% for item in form.items %}
{% set nom_item = 'Item'~loop.index %}
{% endfor %}
How could I do to get alphabet letter in loop ? I can't find a twig function for that.
Simplest solution
{{ range('A','Z')[loop.index0] }}
try with this!
{% set foo = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] %}
{% for index,item in form.items %}
{% set nom_item = 'Item'~foo[index] %}
{% endfor %}

How to use capture cycle odd even and target specific collection item in Jekyll

Hi I'm working on a Jekyll site. I am using a capture cycle for odd and even displays of the collection. I would also like to specifically target which collections are shown on this page.
Here's the code so far:
{% assign sorted = (site.catalog | sort: 'date') | reverse %}
{% for project in sorted limit: 5 reversed %}
{% capture thecycle %}{% cycle 'odd', 'even' %}{% endcapture %}
{% if thecycle == 'odd' %}
{% if project.featured == "1" || project.featured == "3" || project.featured == "5" %}
<div>{{ project.title }} {{ project.subtitle }}</div>
{% endif %}
{% if thecycle == 'even' %}
<div>{{ project.title }} {{ project.subtitle }}</div>
{% endif %}
{% endfor %}
So I would like five items to be shown on this page in total. For the odd cycles, I would like those items to be (1,3,5) to display a certain format, and for the even cycles, I would like those items (2,4) to display a certain format.
The approach I started using would show this variable in the front matter of the collection item:
featured: "1"
Thanks in advance for any help

Show All Color Variants on Collection page in Shopify using Brooklyn Theme

Hey i am using brooklyn theme in my shopify website. I have different products will color variants . When i click on collection page i want to show all color variants of that products as separate products. I am googling since last night any help please.
Look at the codes below.
<ul class="colorlist">
{% for option in product.options %}
{% if option == 'Color' %}
{% assign index = forloop.index0 %}
{% assign colorlist = '' %}
{% assign color = '' %}
{% for variant in product.variants %}
{% capture color %}
{{ variant.options[index] }}
{% endcapture %}
{% unless colorlist contains color %}
{% if variant.available %}
<li id="{{ variant.id }}" title="{{ variant.inventory_quantity }} In Stock" class="instock">{{ color | downcase }}</li>
{% else %}
<li id="{{ variant.id }}" title="Out of Stock" class="outstock" >{{ color | downcase }}</li>
{% endif %}
{% capture tempList %}
{{colorlist | append: color | append: " " }}
{% endcapture %}
{% assign colorlist = tempList %}
{% endunless %}
{% endfor %}
{% endif %}
{% endfor %}
</ul>
The code above can display the available colors of a product in collection page. You can take the same loop structure and display the entire product grid instead of just displaying the variant name.

Twig loop gets mutliplied

Lets say I have some data called 'brands ' in an array past into a twig template like this:
{ 0:1,1:2,2:3,3:4,4:5,5:6,6:7,7:8}
Second array called 'designs' like this
{120:11,123:22,189:32,300:34,400:53,500:63,688:37,799:28}
o/p excepted
12,24,35,38,59,70,45,36
the below code shows what i have tried and the o/p expected for 8 times, but i am getting 64 times the output like
{% for key ,value in brands %}
{% for key,design in designs %}
// for example i need to add the value and design for 8 times
{% set total = value + design %}
{% endfor %}
{% endfor %}
You are incorrectly nesting your loops, and adding each element of designs to each element of brands. You only need one loop:
{% for key, value in brands %}
{% set total = value + designs[key] %}
{% endfor %}
Don't forget you can't access total unless you define it before starting the loop:
{% set total = 0 %}
{% for key, value in brands %}
{% set total = value + designs[key] %}
{% endfor %}
If you're looking for the sum of both of the arrays into one total with varying numbers of elements for brands and designs:
{% set total = 0 %}
{% for key, value in brands %}
{% set total = total + value %}
{% endfor %}
{% for key, value in designs %}
{% set total = total + value %}
{% endfor %}
If you're looking to add together each corresponding element between designs and brands without depending on matching keys - it's a much more difficult process when trying to do it in Twig, and you're missing what Twig is meant for entirely. You should handle this data in the controller or build a better data model to pass to Twig. For example:
$brands = array(0=>1,1=>2,2=>3,3=>4,4=>5,5=>6,6=>7,7=>8);
$designs = array(120=>11,123=>22,189=>32,300=>34,400=>53,500=>63,688=>37,799=>28);
$brands_designs = array();
foreach ($brands as $key => $brand) {
$design_key = key($designs);
$brands_designs[$key] = array(
'brand' => $brand,
'brand_key' => $key,
'design' => next($designs),
'design_key' => $design_key
);
}
return $this->render('AcmeBundle:Folder:template.html.twig', array('brands_designs' => $brands_designs));
Then in your Twig template:
{% for key, value in brands_designs %}
{% set total = value.brand + value.design %}
{% endfor %}
But if you insist on matching each element together and adding them individually...
{% for brandKey, brand in brands %}
{% set outerLoopIndex = loop.index %}
{% for designKey, design in designs %}
{% if outerLoopIndex == loop.index %}
{% set total = brand + design %}
{# do stuff with total here, like {{ total }} #}
{% endif %}
{% endfor %}
{% endfor %}
The above isn't tested, I'm a little worried about variable scope in there. It actually iterates a total of 64 times with your sample set but only outputs on the diagonal (when the outer index == inner index, so 1 == 1, 2 == 2, etc.)

Resources