cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2385
Views
0
Helpful
2
Replies

struggle with DNA Jinja for-loop in templates

pieterh
VIP
VIP

Hi all,
I'm practicing with templates in Cisco DNA center (Version 2.3.3.7-72328)
And now I've been struggling with a Jinja for-loop in DNA templates

DNA center provides a sample template  Multi-Switch Template with Macro
in which the line {% for stack_num in range(1, number_of_stacks) %} is used

this command does not execute as I expect, it runs for a member less then specified
if i specify "4"  it runs for members 1, 2, 3.

part is explainable because the for loop runs from "0" to (but-not-included) the specified variable
specifying "1" as start helps to skip member "0" that not exist, but still the last executed "3" is instead of "4"
-> as such this example template is incorrect

what modification is needed to run until number_of_stacks ?
- simply specifying number_of_stacks +1number_of_stacks +(1) 
   other variants did not help or gave unexpected result (for members 1 to 40 (!) )
- using a in-between variable for number_of_stacks +1 also did not help
searching for Jinja I found many interesting examples and tutorials,
but except from the from "0" to (but-not-included) , Google and I  did not succeed to find something helpful for this issue

any suggestions?

1 Accepted Solution

Accepted Solutions

with above in mind,  Google helped me finding this page 
     python - Finding the sum of numbers in jinja2 template using flask - Stack Overflow

this construction solved my problem:
     {% set range_end = 1|int + number_of_stacks|int %}
     {% for stack_num in range(1, range_end ) -%}
     ...

View solution in original post

2 Replies 2

pieterh
VIP
VIP

looks like number_of_stacks +(1)  performs a concatenation instead (4+1=41)  of the intended addition
how do I make it an addition

with above in mind,  Google helped me finding this page 
     python - Finding the sum of numbers in jinja2 template using flask - Stack Overflow

this construction solved my problem:
     {% set range_end = 1|int + number_of_stacks|int %}
     {% for stack_num in range(1, range_end ) -%}
     ...

Review Cisco Networking for a $25 gift card