TWIG syntax do not show field IF - php

I have the script written in TWIG.
Weight: {{ doc.weight }} kgs<br />
// This part of script shows text information from "detail" and "info" MySQL fields ONLY IF these fields is NOT empty
{% if "" == doc.info_hl %}
{% if '' != doc.detail_code %}
<b>Info: {{ doc.info }}</b>
{% endif %}
{% else %}
Info: {{ doc.info_hl|raw }}
{% endif %}
<br />
{% if "" == doc.detail_code_hl %}
{% if '' != doc.detail_code %}
<b>Details: {{ doc.detail_code }}</b>
{% endif %}
{% else %}
<b>Details: {{ doc.detail_code_hl|raw }}</b>
{% endif %}
Sometimes MySQL field "weight" have values "0.00"
How to modify the code above - DO NOT SHOW "weight" MySQL field IF value in this field "0.00" ?
We can see above how we can do it with empty text fields, but how to do that with decimal fields equals to "0.00" ?
Thanks in advance for any hint to try !

What about simply write
{% if '0.00' != doc.weight %} Weight: {{ doc.weight }} kgs {% endif %}
or even better
{% if not ('0.00' == doc.weight) %}... {% endif %}
?

Please have a look at the is empty test in the Twig documentation. This test operates the same as the PHP empty() function. Here is a handy Type Comparison Table to help you get any idea of what it will return.
If it is expected that this field will only ever contain numbers, or String representations of numbers, you can get around empty('0.00') === FALSE by adding a 0 to the variable:
empty('0.00' + 0) === TRUE
// In Twig: {%if ($value + 0) is empty %}
Here is a list of available Expressions available in Twig Conditionals.

Related

How can I check if a value exists with twig?

I checked if my value exists before printing:
{% if address.company|length %}{{ address.company}}{% endif %}
But still I get an error message:
Key "company" does not exist as the array is empty.
Using the default filter you can easily verify if a company exists and is not empty simultaneously:
{% if address.company|default %}
The company is not empty
{% else %}
The company is empty.
{% endif %}
Try using the empty twig function:
{% if address.company is not empty %}
{{ address.company }}
{% endif %}

Looping on number value TWIG

I would like to loop on this variable that I have created before :
{% set divisionElement = (elementsLength/2)|round|number_format(0) %}
The output of this is a number.
After that I would like to create a loop with this value like that :
{% for i in divisionElement %}
{{dump(i}}
{% endfor %}
When I tried to dump i in my loop I have nothing result.
Try using range, If divisionElement is > 0
{% for i in range(1, divisionElement ) %}
{{ i }},
{% endfor %}

How to check if record exist in respons TWIG - data in table format

I have a multidimensional array where some objects exist and others don't. The whole data has used in page. Then I plan check it in TWIG.
example data:
array:2[
0 => Data1 {
-id: 17
-porodType: "1d"
-name: "Dally promotion"
}
1 => Data1 {
-id: 34
-porodType: "S"
-name: "Special"
}
]
How to check if record with porodType = "1d" exist in respons?
How to display different message for this acction: exist(OK)/no-exist(ERROR)?
When check in TWIG:
{% for d in Data1 %}
{% if d.porodType == '1d' %}
<button class="btn">OK</button>
{% else %}
<button class="btn"">Error</button>
{% endif %}
{% endfor %}
this code result is: <button class="btn">OK</button><button class="btn">Error</button>
but I expected <button class="btn">OK</button> or <button class="btn">ERROR</button>
If you only want to show one button, you'd need to keep track of an error with a flag as you can't break loops in Twig,
{% set error = false %}
{% for d in Data1 %}
{% if d.porodType != '1d' %}
{% set error = true %}
{% endif %}
{% endfor %}
{% if error %}
<button class="btn">Error</button>
{% else %}
<button class="btn">OK</button>
{% endif %}
Using twig's for..if..else may be simpler than the currently accepted answer:
{% for d in Data1 if d.porodType == "1.d" %}
<!-- show error button -->
{% else %}
<!-- show the okay button -->
{% endfor %}
The else clause kicks in when the loop was empty (didn't have any iterations).
See documentation of for tag: https://twig.sensiolabs.org/doc/2.x/tags/for.html

Find if twig array is empty of non-protected values

Given an a php array in a twig template:
object(Drupal\Core\Template\Attribute)#1208 (1) {
["storage":protected]=> array(0) { }
}
How do I check if there are no non-protected elements in the array? The idea is that I can only operate on non-protected values, so I can pretend the array is empty if only protected values are present.
So far, my check is as follows:
{% if attributes is defined and attributes is not empty %}
<div{{ attributes }}>
{{ content }}
</div>
{% else %}
{{ content }}
{% endif %}
In its current form, this displays <div>[Content]</div>. Instead, I'd like to see: [Content]
Any help?
If this is in Drupal 8, you can pass the attributes value through render to find out, like this:
{% if attributes|render %}
<div{{ attributes }}>
{{ content }}
</div>
{% else %}
{{ content }}
{% endif %}
Extend twig
<?php
$twig = new Twig_Environment($loader);
$twig->addFilter(new Twig_SimpleFilter('accessible_properties', 'get_object_vars'));
Use it inside template
{% set public_attributes = attributes is defined ? (attributes|accessible_properties) : [] %}
{% if public_attributes is not empty %}
...
{% else %}
...
{% endif %}

How do I make a simple count loop in Wordpress Timber(Twigg)?

How do I make a simple count loop in Wordpress Timber(Twigg)?
So basically just a loop like this:
($i = 0;0 < 3;i++){
echo $test[i];
}
You could use
{% for value in test %}
{{ value }}
{% endfor %}
that is safer than
{% for i in 0..2 %}
{{ test[i] }}
{% endfor %}
because in second version you have to care about index (is setted? and so on) whereas in the first you don't.
Of course if your final goal is to print only three elements from the array you should consider slice filter
{% for value in test|slice(0, 3) %}
{{ value }}
{% endfor %}

Categories