1

I was going through this solution of pset6 (more comfortable) and I fail to understand the following for loop:

{% for i in range((s2)|length + 1) %}
            <th>{{i}}</th>
{% endfor %}

Specifically, I want to know, what is length, from where are we getting it and why does removing it gives an internal server error.

2 Answers 2

2

Since it's in a jinja "statment", maybe it's a jinja thing. An internet search for 'jinja length' finds this:

length(object)

Return the number of items of a sequence or mapping.
Aliases: count

There are a lot of other results you may find interesting.

3
  • I would like to know why use range((s2)|length + 1) instead of range(s2) Commented Jan 10, 2019 at 5:10
  • I guess it depends on what s2 is. Commented Jan 10, 2019 at 12:51
  • s2 is a string. However, using for s in s2 gives an error (TypeError' object has no attribute 'code'). Commented Jan 11, 2019 at 22:35
1

Jinja, unfortunately, does not support executing arbitrary Python code, such as

len(s2)

Hence, in order to get the length of a string, the use of a filter is required.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .