How to say "if this translation is empty" - php

I want to have this image not showing if the translation is empty or non existing
<img class="partners-logo" src="{{ 'page.image.path' | trans | raw }}">
So can I wrap a logic loop around it as in the following code?
{% if {{ 'page.image.path' | trans }} is not null %}
<img class="partners-logo" src="{{ 'page.image.path' | trans | raw }}">
{% endif %}
Obviously not right? Then how should it be?

You can do something like that :
{% if "page.image.path"|trans != "page.image.path" %}
This will check if the result of the translation is different from the translation key : if a translation key has no translation, filter trans returns the translation key.

Related

How to put liquid code inside a php string

I want to put Liquid code in a metafield with value type string. This is the Liquid code:
{% unless shop.metafields.cmld == blank %}
{%- assign cmld = shop.metafields.cmld -%}
<div class="slider">
{%- for field in cmld -%}
<div>
<img src="{{ field | last }}" />
</div>
{% endfor %}
</div>
{% endunless %}
The Liquid code works fine when I try it on shopify.
The problem here is this error on the 1st row:
{%'(T_CONSTANT_ENCAPSED_STRING), expecting ')' in .....
Any suggestions? It looks like this:
$add_metafield= array(
"metafield" => array(
"namespace"=> $metafield_namespace,
"key"=> "something",
"value" => "{% unless shop.metafields.".$metafield_namespace." == blank %}{%- assign ".$metafield_namespace." = shop.metafields.".$metafield_namespace." -%}<div class="slider">{%- for field in ".$metafield_namespace." -%}<div><img src="{{ field | last }}" /></div>{% endfor %}</div>{% endunless %}",
"value_type" => "string"
)
);
Change from double to single quotes in <div class="slider"> and <img src="{{ field | last }}" />
changing it to -> <div class='slider'> and <img src='{{ field | last }}' />

Print Twig variable x times based upon randomized range

I am using Twig and Timber for a WordPress project. I have the following loop in my template that prints my custom post type titles into a HTML structure.
{% for company in companies %}
{% set dot = "<div class='company-dot'></div>" %}
{% set range = range(10, 20) %}
{{dot}}
{{random(range)}}
<div class="company">
<div class="company-dot dot-active"></div>
<p class="dot-caption">{{ company.title }}</p>
</div>
{% endfor %}
I would like to print my {{dot}} variable x amount of times based upon the number that is generated by {{random(range)}}. How can I do this?
The simplest solution would be to iterate random(range) times with a for loop:
{% for i in 0..random(range(10, 20)) %}
{{ dot }}
{% endfor %}
I don't really know Twig but my guess is that you could to the following:
{% for i in random(range) %}
{{dot}}
{% endfor %}
You already know how to use range, just use it again :
{% set dots_count = random(range) %}
{% for dot_index in range(1,dots_count) %}
{{dot}}
{% endfor %}
{{dots_count}}
Here is a fiddle : https://twigfiddle.com/ko595z

Twig Comparison Operator not behaving as it should

Am trying to add a certain class to an anchor element if a given condition is met using symfony's twig templating engine, The following piece of code is being used in an attempt to achieve this:
{% if colors is defined and colors is not empty %}
{% for keys, c in colors %}
<li>
<a id="{{ keys }}" data-rel="tooltip" data-placement="top" title="{{ c.color|capitalize }}" class="picker-btn{{ (colorData[keys] is defined and colorData[keys]['code'] == c.hexcode) ? ' selected':'' }}" style="background: {{ c.hexcode }}" data-color-id="{{ c.id }}" data-color-text="{{ c.color }}" data-color-code="{{ c.hexcode }}"></a>
</li>
{% endfor %}
{% endif %}
The above code adds the selected class rightfully, to just the first anchor element even when I expect to have 3 anchor elements assigned this class:
{% if colors is defined and colors is not empty %}
{% for keys, c in colors %}
<li>
<a id="{{ keys }}" data-rel="tooltip" data-placement="top" title="{{ c.color|capitalize }}" class="picker-btn{{ (colorData[keys] is defined and colorData[keys]['code'] in colors | keys) ? ' selected':'' }}" style="background: {{ c.hexcode }}" data-color-id="{{ c.id }}" data-color-text="{{ c.color }}" data-color-code="{{ c.hexcode }}"></a>
</li>
{% endfor %}
{% endif %}
The second code fragment adds the selected class to 3 anchor elements because however you wish to look at it, colorData[keys]['code']has keys that exist in the colors array even if the class isn't being added to the right anchor elements. My question is this; if the comparison operator (==) returns true for matched variable values why isn't the first code fragment working? and why is the second code fragment adding this class to the wrong anchor elements?
A snapshot of the colorData array is shown below:
That of the colors array is thus:
About the first example you're comparing:
"#FFFFFF" == "#FFFFFF"
"#222222" == "#795548"
"#01579b" == "#3e2723"
and after this comparsion it finishes 3 times in condition colorData[keys] is defined. What you can do here is to add one more loop.
{% if colors is defined and colors is not empty %}
{% for keys, c in colors %}
<li>
{% set isColorInColorData = false %}
{% for exactColor in colorData %}
{% if exactColor.code == c.hexcode %}
{% set isColorInColorData = true %}
{% endif %}
{% endfor %}
<a id="{{ keys }}" data-rel="tooltip" data-placement="top" title="{{ c.color|capitalize }}" class="picker-btn{{ isColorInColorData ? ' selected':'' }}" style="background: {{ c.hexcode }}" data-color-id="{{ c.id }}" data-color-text="{{ c.color }}" data-color-code="{{ c.hexcode }}"></a>
</li>
{% endfor %}
{% endif %}
Looking to the second example you're comparing a string with an integer.
"#FFFFFF" in [0,1,2]
"#222222" in [0,1,2]
"#01579b" in [0,1,2]
Note that "#FFFFFF" is "equal"(==) to 0 in php!!
So you have to really concentrate on what are you comparing. In twig there is no === operator.
With the first code, you're saying
colorData[keys] is defined and colorData[keys]['code'] == c.hexcode
As you loop over an array of 3 items, keys is going to be 0, 1, 2 (or maybe 1, 2, 3 given it's Twig). So you're saying
colorData[0]['code'] == c.hexcode
i.e. comparing colorData[0] with colors[0], so does #FFFFFF == #FFFFFF, which it does.
Second iteration, you're then comparing colorData[1] with colors[1], so does #795548 == #222222, and so on.

increase size of excerpt in shopify

i am displaying blog articles in some text and at the end Read more button in order to view complete article in my shopify web
this is the code that i am using
<div class="rte">
{% if article.image %}
{% assign image_alt = article.title | escape %}
<p>{{ article | img_url: '1024x1024' | img_tag: image_alt, 'article__image' | link_to: article.url }}</p>
{% endif %}
{% if article.excerpt.size > 0 %}
{{ article.excerpt }}
{% else %}
<p>{{ article.content | strip_html | truncatewords: 100 }}</p>
{% endif %}
</div>
it worked very well now i set only one article to display
{% paginate blog.articles by 1 %}
it displayed only one article.
i just want to increase the short description text of article to 50% with read more button.
is this possible to increase the size of article.excerpt so that i will display more text with read more button
i tried this but this is not work for me
{{ article.excerpt.size = 1200 }}

twig variable inside a function

I am having trouble working around with twig.
I pass a $labelz var on twig, and this $labelz is an array of the form label[0][left], label[1][left], ... label[2][left] so on.
This array, $labelz is actually a _POST var, so when I do some "validation", i want to echo the values stored on this array again back to the form. Hence, I loop it on twig. I can successfully echo the stored value back in a input form using
{% for key, label in labelz %}
<input type="text" name="label[{{ key }}][left]" value="{{ labelz[key]['left'] }}">
{% endfor %}
Now, here is my problem, I am using this function
{% if errors.has('VARIABLE') %}
{{ errors.first('VARIABLE') }}
{% endif %}">
to check if there's an error passed to the page (using errors.has()), then echo the error message if it has (using errors.first()) . Now, with those function I want to do the same to $labelz so I tried using this:
{% for key, label in labelz %}
{% if errors.has(labelz[key]['left']) %}
{{ errors.first(labelz[key]['left']) }}
{% endif %}`
{% endfor %}
but it fails, because as you can see, and note key is a forloop variable of twig, and i have no idea how to make this work. Please help thanks!
turns our ~ solved my problem, it allows me to concatenate string on twig.
Thanks!

Categories