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 :)
I tried Blender's way with
{{ " "|safe*10 }}
it'll generate 10 spaces
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 %}
^
Related
I have a project in django, where I have to display clients in alphabetical order but names of companies can start from upper or lower case.
For now my solution below display names starts from lower case in one section and name starts from the same letter, but uppercase in another section.
{% regroup clients by title.0 as clients_list %}
{% for client in clients_list %}
{{ client.grouper }}
{% for item in client.list %}
{% if item.client_link %}
{{ item.title }}
{% else %}
{{ item.title }}
{% endif %}
{% endfor %}
{% endfor %}
How to display name starts from upper and lower case under one section?
Using Django built-in template filter called title
{{ item.title|title }} {% else %} {{ item.title|title }}
This could rather be straightforward. However, allow me to ask. In the Django yesno inbuilt template tag, the syntax is given as {{ value|yesno:"yeah,no,maybe" }}.
However, I want to out a variable value if it evaluates to true, something like: {{ request.user.profile.mobile_number|yesno:$variable1, 'string' }}.
I have tried this {{ request.user.profile.mobile_number|yesno:"{{ request.user.profile.mobile_number }},No Contact" }} but I keep on getting an error: Could not parse the remainder: ':"{{ request.user.profile.mobile_number' from 'request.user.profile.mobile_number|yesno:"{{ request.user.profile.mobile_number'
Try this:
{% with yesno_args=request.user.profile.mobile_number|add:", No Contact" %}
{{ request.user.profile.mobile_number|yesno:yesno_args }}
{% endwith %}
Alternatively, which may be more clear:
{% if request.user.profile.mobile_number is not None and request.user.profile.mobile_number != '' %}
<span>{{ request.user.profile.mobile_number }}</span>
{% else %}
<span>No Contact</span>
{% endif %}
Hope this helps!
I'm trying to slice loop in django template with variable
USUAL WAY
{% for article in module.module_article_key.module_article_category.article_category_key.all|slice:":2" %}
{{ article.article_title }}
{% endfor %}
WHAT NEEDS
{% for article in module.module_article_key.module_article_category.article_category_key.all|slice:":module.module_article_key.module_article_count" %}
{{ article.article_title }}
{% endfor %}
so we have working variable {{ module.module_article_key.module_article_count }}
normaly this variable gives integer value stored for this module, however wen i use it to slice loop - nothing happens
You need to cast module_article_count to string first then making articleSlice via nested {% with %} and use the resulting template variable in slice filter as follow:
{% with articleCount=module.module_article_key.module_article_count|stringformat:"s" %}
{% with articleSlice=":"|add:articleCount %}
{% for article in module.module_article_key.module_article_category.article_category_key.all|slice:articleSlice %}
{{ article.article_title }}
{% endfor %}
{% endwith %}
{% endwith %}
When running the following jinja code, I only get "Column info" printed. Why the index does not appears ?
{% for field in columns_form %}
{% if 'title_' in field.name %}
<td>Column {{ loop.index }} info</td>
{% endif %}
{% endfor %}
It sounds like the template is being treated as a Django template, not a Jinja template.
Using {{ loop.index }} should work in a Jinja template, but wouldn't work in a Django template, where you would use {{ forloop.counter }} instead.
In case {{ loop.index }} does not work in the latest version, a workaround is to zip the columns_form and range(0, len(columns_form)+1) in the python file as
columns_form_idx = zip(columns_form, range(0, len(columns_form)+1))
In the template file,
{% for field, idx in columns_form_idx %}
{% if 'title_' in field.name %}
<td>Column {{ idx }} info</td>
{% endif %}
{% endfor %}
If I have a list of users say ["Sam", "Bob", "Joe"], I want to do something where I can output in my jinja template file:
{% for user in userlist %}
{{ user }}
{% if !loop.last %}
,
{% endif %}
{% endfor %}
I want to make the output template be:
Sam, Bob, Joe
I tried the above code to check if it was on the last iteration of the loop and if not, then don't insert a comma, but it does not work. How do I do this?
You want your if check to be:
{% if not loop.last %}
,
{% endif %}
Note that you can also shorten the code by using If Expression:
{{ ", " if not loop.last else "" }}
You could also use the builtin join filter like this:
{{ users|join(', ') }}
And using the joiner from https://jinja.palletsprojects.com/templates/#joiner
{% set comma = joiner(",") %}
{% for user in userlist %}
{{ comma() }}{{ user }}
{% endfor %}
It's made for this exact purpose. Normally a join or a check of forloop.last would suffice for a single list, but for multiple groups of things it's useful.
A more complex example on why you would use it.
{% set pipe = joiner("|") %}
{% if categories %} {{ pipe() }}
Categories: {{ categories|join(", ") }}
{% endif %}
{% if author %} {{ pipe() }}
Author: {{ author() }}
{% endif %}
{% if can_edit %} {{ pipe() }}
Edit
{% endif %}