How to preserve whitespace on a multiline string on Jinja template - jinja2

I have this template file:
{%- if wall_area_a1.nfloor0 != '' -%}WALL = {{wall.north}} : SURF= 1 : AREA= {{wall_area_a1.nfloor0}} : EXTERNAL : ORI=N_180_90 : FSKY=0.5 : GEOSURF=0.1{% endif %}
{%- if glazed_area_0_a1.nfloor0 != '' -%}WINDOW={{window_type.north}} : SURF= 6 : AREA= {{glazed_area_0_a1.nfloor0}} : EXTERNAL : ORI=N_180_90 : FSKY=0.5{% endif %}
{%- if door_area_a1.nfloor0 != '' -%}WINDOW=DG_12MM_3_0_ROOF_WINDOW : SURF= 19 : AREA= {{door_area_a1.nfloor0}} : EXTERNAL : ORI=N_180_90 : FSKY=0.5 : ESHADE=1{% endif %}
{%- if wall_area_a1.nefloor0 != '' -%}WALL ={{wall.northeast}} : SURF= 2 : AREA= {{wall_area_a1.nefloor0}} : EXTERNAL : ORI=E_225_90 : FSKY=0.5 : GEOSURF=0.1{% endif %}
{%- if glazed_area_0_a1.nefloor0 != '' -%}WINDOW={{window_type.northeast}} : SURF= 7 : AREA= {{glazed_area_0_a1.nefloor0}} : EXTERNAL : ORI=E_225_90 : FSKY=0.5{% endif %}
{%- if door_area_a1.nefloor0 != '' -%}WINDOW=DG_12MM_3_0_ROOF_WINDOW : SURF= 20 : AREA= {{door_area_a1.nefloor0}} : EXTERNAL : ORI=E_225_90 : FSKY=0.5 : ESHADE=1{% endif %}
Now I have formatted this to return an empty string when the condition is not met. I would like the output to be like this with the white spaces in between removed.
WALL =WT18D : SURF= 2 : AREA= 6.7 : EXTERNAL : ORI=E_225_90 : FSKY=0.5 : GEOSURF=0.1
WINDOW=DG_12MM_2_6_WINDOW : SURF= 7 : AREA= 1.77 : EXTERNAL : ORI=E_225_90 : FSKY=0.5
WINDOW=DG_12MM_3_0_ROOF_WINDOW : SURF= 20 : AREA= 0.72 : EXTERNAL : ORI=E_225_90 : FSKY=0.5 : ESHADE=1
But what happens is it removes all the whitespace. Do you know how to preserve this as a multiline string while removing the whitespace?
WALL =WT18D : SURF= 2 : AREA= 6.7 : EXTERNAL : ORI=E_225_90 : FSKY=0.5 : GEOSURF=0.1 WINDOW=DG_12MM_2_6_WINDOW : SURF= 7 : AREA= 1.77 : EXTERNAL : ORI=E_225_90 : FSKY=0.5 WINDOW=DG_12MM_3_0_ROOF_WINDOW : SURF= 20 : AREA= 0.72 : EXTERNAL : ORI=E_225_90 : FSKY=0.5 : ESHADE=1

Related

How do I trigger autoplay in a video field in Hubspot?

Using Hubspot and under Marketing -> Files and Templates -> Design Tools:
I am using a video in a Template called promo_video. Also, using the copy_snippet produces the code below to be used in module.html.
{% if module.promo_video.player_id %}
{% set max_width = module.promo_video.size_type == 'auto_custom_max' ? module.promo_video.max_width : module.promo_video.width %}
{% set max_height = module.promo_video.size_type == 'auto_custom_max' ? module.promo_video.max_height : module.promo_video.height %}
{% video_player "embed_player"
player_id={{ module.promo_video.player_id }}
type={{ module.promo_video.player_type || 'scriptV4' }}
full_width={{ module.promo_video.size_type == 'auto_full_width' }},
conversion_asset={{ module.promo_video.conversion_asset|tojson|safe }}
autoplay={{ module.promo_video.autoplay }}
hidden_controls={{ module.promo_video.hide_controls }}
loop={{ module.promo_video.loop_video }}
muted={{ module.promo_video.mute_by_default }}
%}
{% endif %}
How do I automatically turn on autoplay for the video in it's page? (video stars when the page loads)
I tried to add "allow" to the line below but the video does not autoplay:
autoplay={{ module.promo_video.autoplay }}
below is a portion of the video in its webpage

Not able to access dictionary value in Macro DBT

I am accessing a dictionary variable which is defined in the marco
{% macro normalize_state(column_name) -%}
{% set states_dict = {
"Alabama" : "AL",
"Alaska" : "AK",
...
....
} -%}
CASE WHEN {{column_name}} IS NOT NULL THEN '{{ states_dict.get(column_name) }}'
ELSE NULL END
{%- endmacro %}
But '{{ states_dict.get(column_name) }}' produced the output None
Macros are compiled (templated) before the query is run. That means that the data in your database doesn't run through the jinja templater.
{{ states_dict.get(column_name) }} looks up the name of the column in your dictionary, not the data it contains.
What you need to do is use your dictionary to write a case statement that performs the get operation. That looks like this:
{% macro normalize_state(column_name) -%}
{% set states_dict = {
"Alabama" : "AL",
"Alaska" : "AK",
...
} -%}
case
{% for k, v in states_dict.items() %}
when {{ column_name }} = '{{ k }}'
then '{{ v }}'
{% endfor %}
end
{%- endmacro %}
Then you need to pass in column_name as a string (quoted) when you call the macro:
select {{ normalize_state("my_column") }}

JSON parse error on line 5: expecting 'STRING', ' , got "undefined'

I keep getting an error on line 5 and am not sure why, my quotation marks seem good.
{
"tickets": [
{
"id": "{{ticket.id}}",
"additional_tags": [{% for cc in ticket.ccs %}"{{ cc.email }}"{% if forloop.last == false %},{% endif %}{% endfor %}]
}
]
}

(Shopify) How to update my inventory message on variant change using current theme JSON + AJAX setup

Background:
I am currently helping a company move their current website to Shopify and am trying to add a custom feature to the product page. I am a web designer, not a developer so I am having trouble implementing this.
Here is my situation:
At this point, our theme allows us to display an "In stock & ready to ship" or an "Out of stock" message on our product page if the variant.inventory_quantity is great than 0 or equal to 0, respectively. In my case, I have added another option that will display a message if the following is true:
section.settings.show_special_order_option and selected_variant.inventory_management and selected_variant.inventory_policy == 'continue' and selected_variant.inventory_quantity == 0
In this case, section.settings.show_special_order_option is a checkbox option I created that when checked will [ideally] display the message. I would like this message to only be displayed if the above conditions are met, and update as each new variation is selected in the event that the conditions are not met.
As of now, this is my code in the product-info.liquid section:
{%- if selected_variant.available -%}
{%- if selected_variant.inventory_management and selected_variant.inventory_policy == 'deny' and section.settings.low_inventory_threshold > 0 -%}
{%- if selected_variant.inventory_quantity <= section.settings.low_inventory_threshold -%}
<span class="product-form__inventory inventory inventory--low">{{ 'product.form.low_stock_with_quantity_count' | t: count: selected_variant.inventory_quantity }}</span>
{%- else -%}
<span class="product-form__inventory inventory inventory--high">{{ 'product.form.in_stock_with_quantity_count' | t: count: selected_variant.inventory_quantity }}</span>
{%- endif -%}
{%- elsif section.settings.show_special_order_option and selected_variant.inventory_management and selected_variant.inventory_policy == 'continue' -%}
{%- if selected_variant.inventory_quantity == 0 -%}
<span class="product-form__inventory inventory inventory--special-order">{{ 'product.form.no_stock_special_order' | t }}</span>
{%- else -%}
<span class="product-form__inventory inventory inventory--high">{{ 'product.form.in_stock' | t }}</span>
{%- endif -%}
{%- else -%}
<span class="product-form__inventory inventory inventory--high">{{ 'product.form.in_stock' | t }}</span>
{%- endif -%}
{%- else -%}
<span class="product-form__inventory inventory">{{ 'product.form.sold_out' | t }}</span>
{%- endif -%}
And this is my JSON Script:
<script type="application/json" data-product-json>
{
"product": {{ product | json }},
"options_with_values": {{ product.options_with_values | json }},
"selected_variant_id": {{ selected_variant.id }}
{%- if section.settings.show_inventory_quantity -%}
,"inventories": {
{%- for variant in product.variants -%}
{%- if variant.available -%}
{%- if variant.inventory_management and variant.inventory_policy == 'deny' and section.settings.low_inventory_threshold > 0 -%}
{%- if variant.inventory_quantity <= section.settings.low_inventory_threshold -%}
{%- capture inventory_message -%}{{ 'product.form.low_stock_with_quantity_count' | t: count: variant.inventory_quantity }}{%- endcapture -%}
{%- else -%}
{%- capture inventory_message -%}{{ 'product.form.in_stock_with_quantity_count' | t: count: variant.inventory_quantity }}{%- endcapture -%}
{%- endif -%}
{%- elsif section.settings.show_special_order_option and selected_variant.inventory_management and selected_variant.inventory_policy == 'continue' -%}
{%- if selected_variant.inventory_quantity == 0 -%}
{%- capture inventory_message -%}{{ 'product.form.no_stock_special_order' | t }}{%- endcapture -%}
{%- else -%}
{%- capture inventory_message -%}{{ 'product.form.in_stock' | t }}{%- endcapture -%}
{%- endif -%}
{%- else -%}
{%- capture inventory_message -%}{{ 'product.form.in_stock' | t }}{%- endcapture -%}
{%- endif -%}
{%- else -%}
{%- capture inventory_message -%}{{ 'product.form.sold_out' | t }}{%- endcapture -%}
{%- endif -%}
"{{ variant.id }}": {
"inventory_management": {{ variant.inventory_management | json }},
"inventory_policy": {{ variant.inventory_policy | json }},
"inventory_quantity": {{ variant.inventory_quantity | json }},
"inventory_message": {{ inventory_message | json }}
}{% unless forloop.last %},{% endunless %}
{%- endfor -%}
}
{%- endif -%}
}
</script>
And finally this is the JS snippet that is executed when there is a variation change:
function _updateinven(newVariant) {
if (!this.options['showInventoryQuantity'] || !newVariant) {
return;
}
var productFormInventoryElement = this.element.querySelector('.product-form__inventory'),
variantInventoryManagement = this.variantsInventories[newVariant['id']]['inventory_management'],
variantInventoryPolicy = this.variantsInventories[newVariant['id']]['inventory_policy'],
variantInventoryQuantity = this.variantsInventories[newVariant['id']]['inventory_quantity'],
variantInventoryMessage = this.variantsInventories[newVariant['id']]['inventory_message'];
productFormInventoryElement.classList.remove('inventory--high');
productFormInventoryElement.classList.remove('inventory--low');
productFormInventoryElement.classList.remove('inventory--special-order');
if (newVariant['available']) {
if (null !== variantInventoryManagement && variantInventoryPolicy === 'deny' && this.options['lowInventoryThreshold'] > 0) {
if (variantInventoryQuantity <= this.options['lowInventoryThreshold']) {
productFormInventoryElement.classList.add('inventory--low');
} else {
productFormInventoryElement.classList.add('inventory--high');
}
} else if (variantInventoryQuantity === 0) {
productFormInventoryElement.classList.add('inventory--special-order');
} else {
productFormInventoryElement.classList.add('inventory--high');
}
}
// We also need to update the stock countdown if setup
var stockCountdown = this.element.querySelector('.inventory-bar');
if (stockCountdown) {
var stockCountdownProgress = Math.min(Math.max(variantInventoryQuantity / parseInt(stockCountdown.getAttribute('data-stock-countdown-max')) * 100.0, 0), 100);
stockCountdown.classList.toggle('inventory-bar--hidden', stockCountdownProgress === 0);
stockCountdown.firstElementChild.style.width = stockCountdownProgress + '%';
}
productFormInventoryElement.innerHTML = variantInventoryMessage;
}
How the function currently works:
As of now, when clicking on a product that is available and has inventory greater than 0, the "In stock & ready to ship" message appears in green (the class 'inventory--high' is what makes it green). When clicking on another variation that is out of stock but set to 'Continue selling when out of stock' the message stays the same, but the color changes to orange (the class 'inventory--special-order' is what makes it orange).
What appears to be the problem:
I have narrowed this problem down to the liquid function and/or JSON script. When viewing the page source, I look at the computed JSON script and it is displaying the same message for all variations when it should be displaying unique messages depending on the conditions set forth. (In other words, every variation in the JSON script has the same inventory message of "In stock & ready to ship" or "Special order: this product will take 7-10 days to arrive" even if there multiple variations in stock and multiple not in stock but able to continue selling).
My question:
Assuming I have provided all the necessary information, I am in need of some guidance as to correct my function to display the proper inventory message for each variation as the user selects different variations. Could someone please help me solve this problem? Please let me know if you need any additional information from me.

Nesting variables in Jekyll/Liquid

Assumption - From what I understand, Liquid works in a way that the variable page.my_key can be compared to a PHP array with name page and key my_key: $page['my_key']. Following this same logic we could compare {{ page.my_key }} with echo $page['my_key'].
And we could compare this front matter:
----
my_key: my_value
----
to this PHP code:
$page['my_key'] = "my_value";
Question - I would like to do something like this:
$page['my_key'] = "my_value";
$page['my_key2'] = "my_value2";
$key = "my_key";
echo $page[$key];
All I can think of is:
----
my_key: my_value
my_key2: my_value2
----
{% assign key = 'my_key' %}
{{ page.{{ key }} }}
However, that does not work... Is something like this possible, though?
Beware : array and hash are two different animals.
Just create a array-hash.md (note I wrote it in markdown for brevity) page in your jekyll. Paste this code. And you will understand how they are different and how to access their items.
---
layout: default
title: array-hash
myArray:
- item 1
- item 2
- one more
# or
myArray2: [ item 1, item 2, one more item ]
myHash:
item1: toto
"item 2": titi
item 3: yoyo
---
{% comment %} +++ Shortcuts
a = page.myArray
h = page.MyHash
h2 = page.myArray2
{% endcomment %}
{% assign a = page.myArray %}
{% assign a2 = page.myArray2 %}
{% assign h = page.myHash %}
## Arrays
page.myArray : {{ a }}
page.myArray with inspect : {{ a | inspect }}
page.myArray with join : {{ a | join:", " }}
page.myArray2 : {{ a2 | inspect }}
### Looping the array
<ul>
{% for item in a %}
<li>{{ item | inspect }}</li>
{% endfor %}
</ul>
### Targeting a specific item in the array
{% comment %} arrays start at zero {% endcomment %}
second element in the array = {{ a[1] }}
Note that {% raw %}{{ a["1"] }}{% endraw %} will not work. You need to pass
an integer and not a string.
Test (not working) : { a["1"] }
## Hashes
page.myHash : {{ h }}
#### looping the hash
{% for item in h %}
{{ item | inspect }}
{% endfor %}
You note that in the loop we get arrays that returns **key as item[0]**
and **value as item[1]**
The loop can then look like :
<ul>
{% for item in h %}
<li>{{ item[0] }} : {{ item[1] }}</li>
{% endfor %}
</ul>
### Targeting a specific item in the hash
**Item1** due to the absence of space in the key name, can both me accessed
by dot notation (h.item1) or bracket notation (h["item1"]).
hash.item1 : {{ h.item1 }}
hash["item1"] : {{ h.["item1"] }}
Item 2 and 3, containing a space in their key string can only be accessed with
bracket notation :
hash.item 2 (not working) : {{ h.item 2 }}
hash["item 2"] : {{ h.["item 2"] }}
hash.item 3 (not working) : {{ h.item 3 }}
hash["item 3"] : {{ h.["item 3"] }}
I think I found the solution:
----
my_key: my_value
my_key2: my_value2
----
{% assign key = 'my_key' %}
{{ page[key] }}
Found it here.