In shopify, I'd like to display the quantity of products next to the collection name, in the sidebar menu
When I add:
{{- link.title | escape -}} {% if link.type == 'collection_link'
%} ({{ link.object.all_products_count }}){% endif %}
-It displays all items count in the collection like this:
Menswear (43) + New Arrivals (43) + Ready To Wear (43) +
T-Shirts Sweaters Trousers
I would like it to display each collection item count, and also in the list that is not drop down:
Menswear (43) + New Arrivals (13) + Ready To Wear (20) +
T-Shirts (10) Sweaters (5) Trousers (5)
Would be lovely to get some help.
Related
I am new to coding and learning for Infrastructure as Code.
Trying to pull info from a variable YAML file within a Jinja file using a for loop. It is working but it is displaying the code two additional times and I am not sure why.
{% for item in underlay[inventory_hostname]['MLAG'] -%}
{% if item == 'Odd' -%}
neighbor 192.168.255.1 peer group LEAF_Peer
{%- else %}
neighbor 192.168.255.2 peer group LEAF_Peer
{% endif %}
{%- endfor %}
Sample of entry in Underlay.yml
leaf1-DC2:
interfaces:
loopback0:
ipv4: 10.2.0.11
mask: 32
loopback1:
ipv4: 10.2.1.11
mask: 32
Ethernet3:
ipv4: 10.2.2.0
mask: 31
Ethernet4:
ipv4: 10.2.2.2
mask: 31
Ethernet5:
ipv4: 10.2.2.4
mask: 31
BGP:
ASN: 65201
spine-peers:
- 10.2.2.1
- 10.2.2.3
- 10.2.2.5
spine-ASN: 65200
MLAG: Odd
The results are:
neighbor 192.168.255.2 peer group LEAF_Peer
neighbor 192.168.255.2 peer group LEAF_Peer
neighbor 192.168.255.2 peer group LEAF_Peer
But should be:
neighbor 192.168.255.2 peer group LEAF_Peer
I took the variable file down to a single device that only has the entry for MLAG once but that still produces two additional lines and do not understand why.
The goal is to create a for loop that looks at MLAG and if it is Odd then produce the one statement or Even produce the other.
This happens because a string is an iterable object in both Jinja and Python.
You can test this doing:
{% for item in 'Odd' %}
Displaying one line (because of `{{ item }}`)
{% endfor %}
That would give you
Displaying one line (because of `O`)
Displaying one line (because of `d`)
Displaying one line (because of `d`)
Your fix is as simple as dropping the for loop, totally:
{% if underlay[inventory_hostname].MLAG == 'Odd' -%}
neighbor 192.168.255.1 peer group LEAF_Peer
{% else -%}
neighbor 192.168.255.2 peer group LEAF_Peer
{% endif %}
Who can tell you how to implement the output of all regions in the name1 group when entering a template named region1a, and when entering a template named region2b, output all regions from the name2 group
I implement it like this:
there is a task that starts template generation:
vars:
AllCountry:
- name1
- name2
name1:
- region1a
- region1b
name2:
- region2a
- region2b
tasks:
- name:
template:
src: "regions.j2"
dest: "{{ item }}.conf"
loop:
- region1a
- region2b
---regions.j2---
regions [{%for count in name1%} "my country = {{count}}", {%end for %}]
this gives the desired output, but only because it is explicitly specified for which name (1 or 2) to output
regions "my country = region1a", "my country = region1b"
For each value specified in the loop, a template configuration file must be generated.
When you specify values in loop region1a and region1b template should generate only one row in the configuration file for region1a.conf
regions "my country = region1a", "my country = region1b"
for region1b generate only one row in the configuration file for region1b.conf
regions "my country = region1a", "my country = region1b"
User β.εηοιτ.βε a more optimal structure was proposed. If convenient, you can use it.
vars:
countries:
country1:
regions:
- region1
- region2
- region3
capital: region1
country2:
regions:
- region4
- region5
capital: region5
Thank you all for your help. Still, I managed to figure it out myself.
Here is the final solution:
{% for country in AllCountry %}
{% if item in lookup('vars', country) %}{% for count in lookup('vars', country) %} "My country = {{ count }}"{% if not loop.last %},{% endif %}{% endfor %}{% endif %}{% endfor %}
I'm working on a configuration script for a certain service and I'd like to have it templated for our configuration management tools (Ansible). There's a particular action however, which seems to be a Jinja2 limitation (if that word is acceptable in this case) which I can't overcome:
{% set min = 0 %}
{% set max = 5500 %}
{% for item in list_of_items %}
for i in {min..max}; do command {{ item }} --arg 1 commnand_stuff $i; done
{% set min = max + 1 %}
{% set max = max * 2 %}
#fi
{% endfor %}
The expected (desired) result is:
- iteration 1 - min = 0, max = 5500
- iteration 2 - min = 5501, max = 11000
..
The actual result is:
- min and max have a constant value through all loop iterations - min=0 and max=5500.
So, how do I modify a global variable in Jinja2 in for loop?
set does not work inside a loop. See assigning a variable inside a loop.
It is possible to use loop.index instead. The template below
{% for item in list_of_items %}
{{ 5500 * (loop.index-1) + 1 }}..{{ 5500 * loop.index }}
{% endfor %}
gives
1..5500
5501..11000
11001..16500
Does anyone know how can I select an item from open check through isl code on micros 3700?
I want to select it and perform a discount on it.
VAR CheckItemIndex : N2
VAR DetailsRow : N2
VAR CheckItems[64] : N10
VAR CheckItemsCount : N2
CheckItemsCount = 0
CheckItemIndex = 1
DetailsRow = 1
WINDOW 8, 75, "TEST WINDOW"
DISPLAY DetailsRow, 2, "CHECK ITEMS"
//dtl_type I Info, M Item, D Discount, S ServiceCharge, T Tender/Media, R ReferenceNumber, C CA Detail
FOR i = 1 TO #numdtlt
IF #DTL_TYPE[i] = "M" //AND BIT(#DTL_STATUS[i], 5) = 0
DetailsRow = DetailsRow + 1
DISPLAY DetailsRow, 2, #DTL_NAME[i], " ", #DTL_OBJNUM[i], " ", #DTL_TYPE[i], " ", #DTL_TYPEDEF[i], " S: ", #DTL_STATUS[i]
CheckItems[CheckItemIndex] = #DTL_OBJNUM[i]
CheckItemIndex = CheckItemIndex + 1
CheckItemsCount = CheckItemsCount + 1
ENDIF
ENDFOR
//I want to select an item here (for example the 2nd one) and perform a discount
LOADDBKYBDMACRO 545 // this is a predefined macro for 100% discount
I have concluded that it is not possible to select an item in the check as a user makes with his hand. So i cant perform a discount on a selected item from check.
A solution for my problem is to calculate the discount amount and perform an open amount discount using a predefined macro.
...
LOADKYBDMACRO key(5, 217)//a predefined discount macro open amount
LOADKYBDMACRO makekeys(DiscountPrice)
LOADKYBDMACRO #KEY_ENTER
...
As far as I know it is not possible to select item from ISL, but you could use ItemDiscount command to discount check item:
var dtl_arr[2]: N10 //dtl indexes array
var disc_val_arr[2]: $10 // discount value array
dtl_arr[1] = 1 //...index of item to discount...
disc_val_array[1] = 50 // eg. 50% discount on item
ItemDiscount 1000053, 1, dtl_arr, disc_val_arr //1000053 is the discount obj num, 1 is the number of items to discount
I'm stuck getting a basic if statement to work on a custom pagination for a jekyll site. Here's my code:
{% if paginator.total_pages > 1 %}
{% for page in (1..paginator.total_pages) %}
{% if page == paginator.page %}
({{ page }})
{% elsif page >= 6 and page <= 10 %}
{{ page }}
{% else %}
{{ paginator.page }}
{% endif %}
{% endfor %}
{% endif %}
Assuming the current page is number 8, I get the following output:
8 8 8 8 8 6 7 (8) 9 10 8 8 8 8 8 8 8
Now if I replace {% elsif page >= 6 and page <= 10 %} with {% elsif page >= (paginator.page - 2) and page <= (paginator.page + 2) %} I get the following output:
8 8 8 8 8 8 8 (8) 8 8 8 8 8 8 8 8 8
Can someone explain why basic arithmetic doesn't work on the variable (paginator.page) and how I can get around this?
OK, total rewrite of my answer, I'll leave the old stuff below for historical reference to the troubleshooting steps.
According to this: Liquid and Arithmetic Liquid has it's own way of capturing variables and doing arithmetic. Thanks to the OP for the find. Just surprised that I couldn't find this in their own documentation.
OK, since Liquid apparently needs and, not && I would try getting the arithmetic out of the if statement.
{% for page in (1..paginator.total_pages) %}
{% assign two_less = (paginator.page - 2) %}
{% assign two_more = (paginator.page + 2) %}
{% if page == paginator.page %}
({{ page }})
{% elsif page >= two_less and page <= two_more %}
{{ page }}
...
I think you might be seeing a problem with the Liquid template language. There have been problems before with and in Liquid, even in strings. See this closed issue: https://github.com/Shopify/liquid/issues/13
I would try this:
{% elsif (page >= (paginator.page - 2)) && (page <= (paginator.page + 2)) %}
and and && are slightly different in ruby, and possibly Liquid has some special significance for and.
old answer
It is basically doing exactly what you tell it to do. You say in the comments "I know the value is 8 because it prints 16 times". That is not valid debugging. All you know is that
page = 8 #at the time of execution
and that your if statement determined that it needed to print that value every time. So you are assuming that "8" is coming from the various branches of your if statement, but it is far more likely that your if statement keeps evaluating to the branch that prints "8". The simplest debugging you can do here is add:
{% if paginator.total_pages > 1 %}
{% for page in (1..paginator.total_pages) %}
{% if page == paginator.page %}
({{ page }})
{{ puts "if" }}
{% elsif page >= (paginator.page - 2) and page <= (paginator.page + 2) %}
{{ page }}
{{ puts "else if" }}
{% else %}
{{ paginator.page }}
{{ puts "else" }}
{% endif %}
{% endfor %}
{% endif %}
I don't use Jekyll so I am not 100% sure of my syntax, but you see what I'm trying to achieve? This will tell you where in the if statement your output value is coming from. If it is coming from the arithmetic as you suspect, it will tell you that. What I suspect though, is that you will see it never gets into that part of the if as you assume. Also, I'm not sure why you are getting
8 8 8 8 8 6 7 (8) 9 10 8 8 8 8 8 8 8
Do you want the first five and the last seven elements to evaluate to 8 and not:
1 2 3 4 5 6 7 (8) 9 10 11 12 13 14 15 16 17
Also just to be sure you're getting the type of variable return you expect change
(paginator.page - 2) and page <= (paginator.page + 2)
to:
(paginator.page.to_i - 2) and page <= (paginator.page.to_i + 2)
So my answer is pretty much what mcfinnigan said, arithmetic with variables does work. So you are either not getting an integer from the method paginator.page, or there is some other flaw in the if statement's logic.
edit-----
(removed part about "paginator.number" as it doesn't seem to apply to user's case)
I can expand and improve this answer if you can do the debugging I suggested and post the output.