OR condition in django templates - django-templates

Is there a way to implement conditional OR inside templates?
{% if %} would see if true or not...but what i'm looking for is
to implement smthing when {% if %} OR {% if %}..thanks

in Django 1.2, you can use OR inside an IF tag...see the built-in template tags
{% if var1 == 'val1' or var2 == 'val2' %}

You can use the firstof template tag. It works similar to an "or":
var1 or var2
will use var1, if it is True or var2, if it is False. Exacly like this statement in the template:
{% firstof var1 var2 %}

Related

DBT Macro With Parameter IF statement eval

I am trying to create a sql template using a macro with one parameter, the if condition does not evaluate to true when passed TABLE1 or TABLE2
{% macro cloud_test_results_get_standard_columns(modelName) %}
result,
Length,
estimatedLength as estimatedLength,
{% if ‘{{modelName}}’ == ‘TABLE1’ %}
TABL1_COL1,
TABL1_COL1,
TABL1_COL1,
{% elif ‘{{modelName}}’ == ‘TABLE2’ %}
TABL1_COL1,
TABL1_COL1,
TABL1_COL1,
{% else %}
TABL_DEFAULT1,
TABL_DEFAULT2,
TABL_DEFAULT3,
{% endif %}
{% endmacro %}
please disregard, had to use modelName instead of ‘{{modelName}}’ inside if block

Django template syntax error: could not parse remainder % 2

I am getting a TemplateSyntaxError: "could not parse remainder % 2 from num%2":
{% if num%2 ==0 %}
{{"Even"}}
{% else %}
{{"Odd"}}
{% endif %}
You can't use arbitrary Python expressions in Django templates. You should create a custom filter for them.
However, for your expression there is a built-in tag divisibleby. From its example:
{{ value|divisibleby:"2" }}
If value is 4, the output would be True. So the final answer looks like (untested):
{% if num|divisibleby:"2" %}
Even
{% else %}
Odd
{% endif %}

Liquid: How to assign the output of an operator to a variable?

I'm working with Liquid templates for Shopify. I want some elements to show up only if the month happens to be December. Since there are multiple elements that need this, I want to set a variable at the top of the document and refer to it later. Here's what I've got that's working:
<!-- At the top of the page -->
{% assign month = 'now' | date: "%m" %}
{% if month == "12" %}
{% assign isDecember = true %}
{% else %}
{% assign isDecember = false %}
{% endif %}
<!-- Only show in December -->
{% if isDecember %}
Happy Holidays
{% endif %}
This works (for testing I change "12" to the current month) but it is pretty ugly. In most languages I would do something like this:
{% assign isDecember = (month == "12") %}
Liquid doesn't accept parentheses, so obviously this won't work. And it doesn't work without parentheses either. The documentation has examples for using operators and for assigning static values to variables, but nothing about combining the two.
I can assign the output of a | filter to a variable, but there don't seem to be filters to cover every operator (or even the necessary "=="), so that's unsatisfactory.
Is there any way to assign the output of an operator to a variable in Liquid?
There isn't a way to do that elegantly and according to this, they won't support ternary operators. There's a mention of someone trying a similar thing.
A slightly shorter/different version would be:
{% assign month = 'now' | date: "%m" %}
{% liquid
case month
when '12'
assign isDecember = true
else
assign isDecember = false
endcase %}
You could altogether avoid using an intermediary boolean flag variable isDecember as Liquid assign with only boolean variables seems to not working within if/endif. Here are the solutions.
Just use plain strings:
{% assign month = 'now' | date: "%m" %}
{% if month == "12" %}
Happy Holidays
{% endif %}
Or use plain string assignments (not boolean values assignments) within ifs:
{% if month == "12" %}
{% assign phrase = "Happy Holidays" %}
{% else %}
{% assign phrase = "Happy usual time of the year" %}
{% endif %}
Now my message to you is: {{ phrase }}
Still want unsing intermediary isDecember? If you would put some dummy text assignment within either clause of if/else that would work too.
{% if month == "12" %}
{% assign dummy = "summy" %}
{% assign isDecember = true %}
{% else %}
{% assign isDecember = false %}
{% endif %}
Hope that helps.

How to use variables in Twig filter 'replace'

Handing over an array from php of form
$repl_arr = array('serach-string1' => 'replace1', ...)
to a Twig template I would like to replace strings in a Twig variable per replace filter similar to this:
{{ block | replace({ repl_arr }) }}
That does not function and neither a variable loop like
{% for key,item in repla_arr %}
{% set var = block | replace({ key : item }) %}
{% endfor %}
does. What is wrong with it? How could it work?
Either you pass the whole array, or you loop the replaces.
But when looping the replaces you need to wrap key and value in parentheses to force interpolation of those
{% set replaces = {
'{site}' : '{stackoverflow}',
'{date}' : "NOW"|date('d-m-Y'),
} %}
{% set haystack = '{site} foobar {site} {date} bar' %}
{{ haystack | replace(replaces) }}
{% set output = haystack %}
{% for key, value in replaces %}
{% set output = output|replace({(key) : (value),}) %}
{% endfor %}
{{ output }}
fiddle

Less than condition in if loop not working - Liquid HTML

In liquid html greater than condition works well in if loop
{% assign var1 = product.extended_attributes.inventory | plus: 0 %}
{% if var1 > 5 %}
test text
{% endif %}
This condition works. But i want to check for less than condition
{% assign var1 = product.extended_attributes.inventory | plus: 0 %}
{% if var1 < 5 %}
test text
{% endif %}
This code breaks the liquid html syntax in < so this condition not working.
Note: this happens on blueshift email template. Not sure about others.
It probably is an app specific bug. Try this instead
{% assign var1 = product.extended_attributes.inventory | plus: 0 %}
{% unless var1 >= 5 %}
test text
{% endunless %}