Check for NaN in nunjucks template? - nunjucks

I'm trying to display 'n/a' instead of NaN in a popup. Something like:
{% if value == NaN %}
n/a
{% endif %}
I realize I can always catch it earlier on before the template is rendered but Is there was a way to check for NaN values in the template?

Here is the logic for a custom filter since there is not a built in filter to check for NaN:
nunjucks.configure().addFilter('nan', function(num) {
if (isNaN(num)){
return 'n/a';
}
return num;
});
Then the usage is the same as for any filter:
{{ num | nan }}

Related

Why does Jinja2 render the string "on" to boolean True?

I'm using Jinja2 with YAML and have the following structure:
{%- set example = [ (20, "on"), (40, "off")] %}
- name: example_yaml
loop:
{%- for value, state in example %}
- TheNumber: {{ value }}
TheState: {{ state }}
{%- endfor %}
When the first loop is rendered, TheNumber is correct with 20, but TheState ends up being True. I've looked through the documentation and have tried adding a string filter like this:
{{ state | string }}
But that did not work either. I have also Tried switching the string "on" to something else like "StateShouldBeOn" just to test with. With that I get what I expect TheState = "StateShouldBeOn".
My question is, why is it that "on" renders to a boolean value?
Try to use 'on' instead of "on". That should help.

if else and if else not working in ansible template

I am checking for pushing data based upon variables set true or false.
My varaibles file which is json file has
{
"isfs" : True
}
and my template (t2.j2)has below condition
{% if '{ isfs | d() | bool }' -%}
<perform operation >
{% else -%}
<perform 2 operation
Every time it executes <perform opertaion >even if change value of json file "isfs": False.
Please pour some suggestion.
I tried d(true) and also I tried with elif condition nothing helping me
just test the variable. This shoud do what you want. Any True or true value is supposed to return true.
{% if isfs %}
cast if needed:
{% if isfs | bool %}
edit: I think i misunderstood that you are evaluating against a json file that contains '{ "isfs" : True }'. You'll need to parse the json file to get the value. Considering the json file content is in the json_file variable:
{% if json_file.isfs %}

How to Modify Discount Code Logic Shopify

I have a discount code which gives 40% discount when there are more than 50 items, but this also works if I have for example 49 items + 1 other item. I want to make the code work only for the single items that have a quantity over 50.
I assume this would be pretty straight forward if I could access this logic myself. Can someone point me into whereabouts could I find these codes for my discounts?
I would also want my discount code to give 40% for items over 50 quantity, and 50% for items over 200 quantity.
I have found something called "Shopify GraphiQL App" which, if I understand correctly, would help me to modify these discount codes internally.
Any help is appreciated.
Thanks
EDIT
I have been playing a bit with GraphiQL, and I have found the priceRule has a field called: allocationMethod (PriceRuleAllocationMethod).
The value of this is ACROSS. Would my discount code have the desired behaviour if I manage to change this to EACH?
How can I modify this, I haven't been able to find an example.
EDIT 2
I have tried the following, but for some reason the allocation method is not updated. Can someone explain to me what is happening here?
I think you can do this using Liquid in the theme.liquid file:
{% assign num = 0 %}
{% for item in cart.items %} #finding how many items in cart
{% capture temp %}{{ num | plus: item.quantity }}{% endcapture %}
{% endif %}
{% assign num = temp %}
{% endfor %}
{% assign new_num = num | plus: 0 %} #converting string to integer
{% if new_num >= 50 %}
{% assign price = checkout.subtotal_price %}
{{ price | times: 0.40 }} #40 percent
{{ checkout.subtotal_price | minus: price }}
{% endif %}
Edit: Maths was wrong in first edit, that should be correct now.

`include` statement in `for` loop does not work

// item.html
test
// main.html
{% for i in range(1, 4) %}
abc
{% include "item.html" %}
def
{% endfor %}
{% include "item.html" %}
The output is:
abcabcabctest (three times abc and one time test). It means that for some reason include statement in loop does not work and also anything after the statement, but still inside loop, is not rendered either. However include outside of loop does work. This is almost same as example from docs so I have no idea what might be wrong with this code.
Using nunjucks v3.1.2.
I tested it and it looks like everything is fine.
// app.js
var nunjucks = require("nunjucks");
var env = nunjucks.configure();
var res = env.render("template.html");
console.log(res);
// template.html
{% for i in range(1, 3) %}
{% include "partial"+ i + ".html" %}
{% endfor %}
{% include "partial1.html" %}
// partial1.html
AAA
// partail2.html
BBB
I had the same issue, and I solved it by changing the web loader to synchronous:
this._nunjucksEnvironment = new nunjucks.Environment(
new nunjucks.WebLoader(this.file(""), { async: false }),
{
trimBlocks: true,
lstripBlocks: true
}
);

How to add a filter to entityview in Liquid template

I'm using entityview to retrieve records from CRM using a liquid template. This is the code I use.
{% entityview logical_name:'new_consul', name:"Most Recent Consul" %}
{% assign cons = entityview.records %}
{% for item in cons %}
But I would like to add a filter to limit to 5 the results, so if I use this code:
{% assign count = count | default: 5 %}
{% assign recent_cons = entityview.records | recent: count %}
I get following error: Liquid syntax error: Error - Filter 'recent' does not have a default value for 'lang' and no value was supplied
What makes me think that filter should is applied correctly, so I then tried with:
{% assign languagecode = 'English' %}
{% assign recent_cons = entityview.records | recent: count,languagecode %}
But then I get this error: Liquid error: Missing a valid input parameter. Parameter name: input
How can I apply the filter to this entitiview query correctly?
Ok, found it on the adxstudios Portal documentation.
https://community.adxstudio.com/products/adxstudio-portals/documentation/configuration-guide/liquid-templates/tags/crm-entity-tags/#page_size
{% entityview logical_name:'new_consul', name:"Most Recent Consult", page_size:5 %}

Resources