06-30-2023 03:39 AM
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 +1 , number_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?
Solved! Go to Solution.
06-30-2023 04:56 AM
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 ) -%}
...
06-30-2023 04:29 AM
looks like number_of_stacks +(1) performs a concatenation instead (4+1=41) of the intended addition
how do I make it an addition
06-30-2023 04:56 AM
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 ) -%}
...
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide