while writing an application in django, I've encountered a problem. I want to make page-number links, with current page not being a link. So in template I do this:
{% for i in pages %}
{% if i == curr_page %} {{ i }}
{% else %} {{ i }}
{% endif %}
Only problem? Jinja doesn't seem to notice two numbers being equal. I've changed the 2nd line to {% if i != curr_page %} {{i}}!={{curr_page}} and got "... 5!=6 6!=6 7!=6 ...".
What should I do?
Because they are not of same data type. In your view, cast them to int before passing to context dict.
pages = list(map(int, pages))
curr_page = int(curr_page)
Related
I want to check if a condition in the linked database is true and then execute some code but I am getting error such as
jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'
{% for prod in prod %}
{% if {{prod.sh}} is 1 %}
<pre>Lines to come if true</pre>
{% endif %}
{% endfor %}
The extensive Jinja2 documentation can be found at https://jinja.palletsprojects.com/en/2.11.x/templates/
About your code - I spot two problems.
You should not use for prod in prod - rather, but something like for product in products, ie name them differently.
You do not have to put angle brackets around prod.sh. You would do this only when the variable is referenced directly within the HTML code.
So a working code could look like:
{% for prod in prod %}
{% if prod.sh == 1 %}
<pre>Lines to come if true</pre>
{% endif %}
{% endfor %}
I'm trying to make a template which iterates over a list to check if a string exists. if it does then something happens, if the string isn't in the list, something else should happen. The issue is the result is repeated for all the lines in the list, which i do not want.
e.g.
The list is some simple yaml
LIST:
- A
- B
- C
Jinja looks like
{% for line in mylist %}
{% if 'A' in line %}
{{ A }} Was found in the list
{% else %}
{{ A }} Was not found
{% endif %}
{% endfor %}
So when 'A' matches i get this
A Was found in the list
A Was not found
A Was not found
And when 'A' does not match i get this:
A Was not found
A Was not found
A Was not found
Basically i need to stop it iterating over the list and just do one thing if it matches or one thing if it doesn't match
So if 'A' matches i need it to just do
A was found
If 'A' doesn't match i need it to just do A was not found
Use some kind of flag variable:
{% set ns = namespace(found=false) %}
{% for line in mylist %}
{% if 'A' in line %}
{% set ns.found=true %}
{% endif %}
{% endfor %}
{% if ns.foo %}
{{ A }} Was found in the list
{% else %}
{{ A }} Was not found
{% endif %}
I'm passing a dictionary in the context in my views.py to my html template. How do I know access a value in the template based on a particular key. For instance I'd wanna do something like {{ dictionary.keyname.value }} but I don't know the correct syntax and for some reason I can't seem to find the documentation.
I want to achieve the same effect as this without having to use a for loop:
<b>Calories</b>
{% for key, value in output.items %}
{% if key == "calories" %}
{{ value }}
{% endif %}
{% endfor %}
You just want {{ output.calories }}.
I want to use a value in my frontmatter to specify a data file to loop through, but I can't get this to work.
I have a data file in _data/sidebars/sidebar1.yml. It contains:
- first
- second
- third
On a page I have this frontmatter:
---
title: My page
sidebar: site.data.sidebar.sidebar1
---
I want to use this code:
{% for entry in page.sidebar %}
* {{entry}}
{% endfor %}
However, this doesn't work. I've tried a number of things (involving assign and capture tags to define the page.sidebar content, but nothing seems to work).
The only thing that works is to do this:
{% if page.sidebar == "site.data.sidebars.sidebar1" %}
{% assign sidebar = site.data.sidebars.sidebar1 %}
{% endif %}
{% for entry in sidebar %}
* {{entry}}
{% endfor %}
However, I would like to avoid this extra code with the if statement, since it's easy to forget this and I would like to automate this more.
Any ideas on how to make this work?
You have a typo in your front matter. It's :
sidebar: site.data.sidebars.sidebar1
not
sidebar: site.data.sidebar.sidebar1
You can even be less verbose.
In your front matter : sidebar: sidebar1
In your code :
{% assign sidebar = site.data.sidebars[page.sidebar] %}
{% for entry in sidebar %}
{{ entry | inspect }}
{% endfor %}
I want to render the “Tags:” label only if the post has actual tags. When I write
{{post.tags | size}}
I get the number of tags. However, if I put it in an {% if %} condition:
{% if post.tags | size != '0' %}
it always yields true. I worked around this problem with
{% capture tagsize %}{{post.tags | size}}{% endcapture %}
{% if tagsize != '0' %}
but it seems very ugly. Is there a better way to do this?
Note: I use GitHub Pages, so using a plugin – other than being an overkill – is not an option. My Jekyll version is 2.0.4 as specified here as of date.
By default {{ post.tags }} == empty array.
As we cannot do {% if post.tags != [] %}, we just add array: [] in _config.yml.
We can now do :
{% if post.tags != site.array %}
Do something
{% endif %}
tag size returns an integer and not a string so you should compare against 0 and not '0'
{% if page.tags.size != 0 %}
<!-- some code to render tags -->
{% endif %}