Twig : translation for element of array displayed via split - arrays

Let's say I have this kind of arrays :
<?php
$monsterOne['statistics'] = array('attack' => 15, 'defense' => 20, 'speed' => 5);
$monsterTwo['statistics'] = array('attack' => 10, 'defense' => 0, 'speed' => 7);
And I want to display, via twig :
monsterOne : 15 en Attaque, 20 en Défense, 5 en Vitesse
monsterTwo : 10 en Attaque, 7 en Vitesse
How can I achieve this ?
I tried this, and it worked, but the commas between each statistic are not present.
{% if statistics.attack is defined %} {{statistics.attack}} {{"en Attaque" | trans}} {% endif %}
{% if statistics.defense is defined %} {{statistics.defense}} {{"en Défense" | trans}} {% endif %}
{% if statistics.escape is defined %} {{statistics.escape}} {{"en Vitesse" | trans}} {% endif %}
I think that I should use something like this, but I don't know how to put the translation inside :
{{ statistics|join(', ') }}

I found the answer. The trick was to parse the array via a loop and use loop.last :
{% for statistic, value in statistics %}
{% if statistic == 'attack' %} {{ value }} {{ 'en Attaque' | trans }}{% endif %}
{% if statistic == 'defense' %} {{ value }} {{ 'en Defense' | trans }}{% endif %}
{% if statistic == 'speed' %} {{ value }} {{ 'en Vitesse' | trans }}{% endif %}
{% if loop.last == false %}, {% endif %}
{% endfor %}">

Related

CRAFT CMS 3 loop matrix field - variable entry does not exist

I'm trying to loop a matrix field that has one block containing 3 items.
{% for block in entry.galeria.type('itemsGaleria') %}
{% if block.titulo|length %}
{{ block.titulo.first }}
{% endif %}
{% endfor %}
But craft always throws the error variable entry does not exist.
I read the matrix section from craft 3 docs but cannot fix this problem.
Any clues?
Well, as there where no sugestions, and i never give up, i figured out for myself :)
Here it is:
{% set entries = craft.entries.section("galeria").all() %}
{% for entry in entries %}
{% for block in entry.galeriaMatrix.all() %}
{% switch block.type %}
{% case "itemsGaleria" %}
{% for image in block.fotografia %}
<img src="{{image.url}}" alt="{{image.title}}" />
{% endfor %}
{{ block.titulo }}
{{ block.texto }}
{% default %}
{% endswitch %}
{% endfor %}
{% endfor %}
It does what i need, which is loop all the entrances in the matrix block field.

How do i array a collection in 3 cols in liquid by a status

I wanna array my collection in 3 cols, for 3 different statuses. And if there isn't any gig/item with that status, it has to say ' No projects'
I have tried this:
<div class="col-sm">
<h2>Up next</h2>
{% assign next = site.gigs | gig.status == 'Next' | sort: gig.date %}
{% if next.gigs.size == 0 %}
No projects
{% else %}
{% for gig in next %}
{{ gig.title }}
{% endfor %}
{% endif %}
</div>
<div class="col-sm">
<h2>Working on</h2>
{% assign on = site.gigs | gig.status == 'On' | sort: gig.date %}
{% if on.gigs.size == 0 %}
No projects
{% else %}
{% for gig in on %}
{{ gig.title }}
{% endfor %}
{% endif %}
</div>
<div class="col-sm">
<h2>Done</h2>
{% assign done = site.gigs | gig.status == 'Done' | sort: gig.date %}
{% if done.gigs.size == 0 %}
No projects
{% else %}
{% for gig in done %}
{{ gig.title }}
{% endfor %}
{% endif %}
</div>
But it just arrays all of the gigs/items :(
Maybe it can be done in a way more simple way.
I don't know if you could make one compact liquid code and array 3 columns by counting the number of different statuses.
Help!
Ordering
Our statuses are "Next", "On" and "Done". They must be ordered in such order.
As this is not an alphabetical sort order, we need to define this order by ourself.
In _config.yml :
status-order:
-
name: "Next" ### status as it is set on gigs (!!! Case-Sensitive !!!)
display: "Up Next" ### status column header to display
-
name: "On"
display: "Working On"
-
name: "Done"
display: "Done"
We can now loop over site.status-order and get our gigs in desired status order.
{% for status in site.status-order %}
{{ status.name }} - {{ status.display }}
{% endfor %}
Presenting
As your current code is a little repetitive, we can factor it like this :
{% for status in site.status-order %}
{% assign items = site.gigs | where: 'status', status.name | sort: date %}
<div class="col-sm">
<h2>{{ status.display }} ({{ items.size }})</h2>
{% if items.size > 0 %}
<ul>
{% for item in items %}
<li>{{ item.title }}</li>
{% endfor %}
</ul>
{% else %}
No project
{% endif %}
</div>
{% endfor %}
Note
You must be sure to set status with the right case (eg : "Next" an not "next").
And the right type. If you set status: On it is understood as the true boolean value, not the string "on". In this case the correct status expression is status: "On". It must be quoted or double quoted to be understood as "On" string.
Any item with incorrectly cased or typed status expression will not appear in our listing.

Shopify liquid product.thumbnail.liquid repeats block even there is no loops

[![enter image description here][1]][1]My job Is too simple i.e just to add Amazon url in a div block in product thumbnail.liquid.I Have added just simple div, then I found that same div is repeated twice. I have also checked that there is no forloop found still how does it is repeating .Today I found the file called product-loop.liquid which has for loop and product thumbnail.liquid is included. What I need to do if I need to show amazon link block only once? Entire file is in gist link.Thanks.
product-loop.liquid
{% assign product_found = false %}
{% assign skip = false %}
{% assign collection_group = products | map: 'id' %}
{% assign collection_group_thumb = collection_group | append : 'thumb' %}
{% assign collection_group_mobile = collection_group | append : 'mobile' %}
{% capture new_row %}
<br class="clear product_clear" />
{% endcapture %}
<div itemtype="http://schema.org/ItemList" class="products">
{% for product in products limit: limit %}
{% if product.id == skip_product.id or skip == true %}
{% assign product_found = true %}
{% else %}
{% if forloop.rindex0 == 0 and product_found == false and forloop.length != products.count and template != 'search' %}
{% assign skip = true %}
{% else %}
{% include 'product-thumbnail', sidebar: sidebar %}
{% if products_per_row == 2 %}
{% cycle collection_group: '', new_row %}
{% elsif products_per_row == 3 %}
{% cycle collection_group: '', '', new_row %}
{% elsif products_per_row == 4 %}
{% cycle collection_group: '', '', '', new_row %}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
</div>
may be you including product.thumbnail in a section and that section have loop or conditional logics.

Twig check if value is in array

I'm having trouble to check if a value is present in an array with Twig.
I want to hide a shipping method in a checkout if there's a certain product in the cart.
I can only use Twig code so I have to find a logic in that.
So let's say when product ID 1234 is in cart then I want to hide #certain_div
So what I have is this ->
{% if checkout %}
{% set array = theme.sku_shipping_rule | split(',') %}
// theme.sku_shipping_rule = a text string like 1234, 4321, 5478
{% if checkout.products %}
{% for product in checkout.products %}
{% if product.sku in array %}
<style>
#certain_div {
display: none;
}
</style>
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
The problem I'm facing is that it seems my code always returns true. So even if the product.sku doens't match a value in the array it still hides #certain_div. I've tested that with placing {{ product.sku }} just before <style>.
What do I wrong?
Any help greatly appreciated!
UPDATE:
I've updated the question/code to show what's happening
{% if checkout %}
{% set skuToCheck = theme.sku_shipping_rule | split(',') %}
{% set skuInCart = [] %}
{% if checkout.quote.products %}
{% for product in checkout.quote.products %}
{% set skuInCart = skuInCart | merge([product.sku]) %}
{% endfor %}
{% endif %}
{% for myVar in skuInCart %}
{{ myVar }}<br/>
{% endfor %}
// this prints
PSYGA1 // where this sku should NOT match
FP32MA4
{% for myVar in skuToCheck %}
{{ myVar }}<br/>
// this prints
FP32LY4
FP32STR4
FP32MA4
{% if myVar in skuInCart %} // also tried with | keys filter
{{ myVar }} is found
{% endif %}
{% endfor %}
{% endif %}
So what I did is placing the sku's from the products which are in the cart in an array skuInCart. Next I want to check if myVar is present in the skuInCart array. If so print myVar is found.
What happens is that you should expect that it prints only the matching results. However it actually prints all values present skuInCart (using keys filter) or completely blank without using keys filter.
What you are doing in theory should work, have a look a this fiddle example to show you a working demonstration:
https://twigfiddle.com/yvpbac
Basically:
<div id="certain_div">
This should not show up
</div>
{% set searchForSku = "890" %}
{% set productSkuArrayString = "1234,4567,890" %}
{% set productSkuArray = productSkuArrayString|split(',') %}
{% if searchForSku in productSkuArray %}
<style>
#certain_div {
display: none;
}
</style>
{% endif %}
<!-- New Trial -->
<div id="certain_div">
This should show up
</div>
{% set searchForSku = "891" %}
{% set productSkuArrayString = "1234,4567,890" %}
{% set productSkuArray = productSkuArrayString|split(',') %}
{% if searchForSku in productSkuArray %}
<style>
#certain_div {
display: none;
}
</style>
{% endif %}
Will result in:
<div id="certain_div">
This should not show up
</div>
<style>
#certain_div {
display: none;
}
</style>
<!-- New Trial -->
<div id="certain_div">
This should show up
</div>
You can use iterable to check if a variable is an array or a traversable object:
{% if items is iterable %}
{# stuff #}
{% endif %}

Liquid (Shopify) scope questions

I'm trying to set two variables outside of a few nested loops (for loops) and then check or reassign the variables inside the for. Basically, I'm trying to check for current_tags and display links to any tags under those tags (in the hierarchy I've got). Shopify seems to only support one level of hierarchy (collections and their associated products), but I'm trying to use these tags to make it look like I've got multiple levels of ways of classifying products. So shirt, then red shirt/brown shirt, then red silk shirt/brown silk shirt, as a shitty example.
Here's some code:
{% assign showtag = false %}
{% for link in linklists[settings.main_linklist].links %}
{% if linklists[link.handle] == empty %}
{% else %}
{% for link in linklists[link.handle].links %}
{% if linklists[link.handle] == empty %}
{% else %}
{% for link in linklists[link.handle].links %}
{% if linklists[link.handle] == empty %}
{% else %}
{% capture temp_tag %}{{current_tags.first | replace: '-', ' '}}{% endcapture %}
link {{link.title}}<br>
temp {{temp_tag}}<br>
{% if showtag == true %}
<li>{{ tag | highlight_active_tag | link_to_tag: tag }}</li>
{% endif %}
{% if link.title == temp_tag}
{% assign showtag = true %}
{% endif %}
{% endif %}
{% for link in linklists[link.handle].links %}
{% capture temp_tag %}{{current_tags.first | replace: '-', ' '}}{% endcapture %}
{% if linklists[link.handle] == empty %}
{% if showtag == true %}
<li>{{ tag | highlight_active_tag | link_to_tag: tag }}</li>
{% endif %}
{% if link.title == temp_tag %}
{% assign showtag = true %}
{% endif %}
{% else %}
{% if showtag == true %}
<li>{{ tag | highlight_active_tag | link_to_tag: tag }}</li>
{% endif %}
{% if link.title == temp_tag %}
{% assign showtag = true %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
The above is a little messy but is properly indented on the website. Anyway, it seems like the variable showtag loses scope and cannot be found in the for loops - any solutions?

Resources