'How to print spaces from inside a variable in jinja2
I am trying to print a variable number of spaces stored in a variable in jinja2, but jinja is escaping the contents of the variable. So when I store   in the variable it gets expanded to   thus printing the characters   on the page instead of the spaces.
Here is my code:
{% macro show_message(parent_id,count) %}
{% set clist = get_message(post.id,parent_id) %}
{% set countr = count + 1 %}
{% set prefix = '-'*countr %}
{% set prefix2 = " "*countr %}
{% if clist is defined %}
{% for c in clist %}
{{ prefix2 }} Author: {{ get_author(c.user_id) }} <br/>
{{ prefix }}
{{ c.message }}
<br/><br/>
{{ show_message(c.id,countr) }}
{% endfor %}
{% endif %}
{% endmacro %}
Does anyone have a clue how to make this work?
Thanks :)
Solution 1:[1]
Mark it as safe with the |safe
filter to prevent auto-escaping:
{{ prefix2|safe }}
You also need to include the semicolon at the end of each entity:
{% set prefix2 = " "*countr %}
^
Solution 2:[2]
I tried Blender's way with
{{ " "|safe*10 }}
it'll generate 10 spaces
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Blender |
Solution 2 | lei dong |