Template Designer Documentation — Jinja Documentation (3.1.x)
jinja.palletsprojects.com [1]
html [2] code generated by my jinja templates generally look half garbage because of indents and whitespace all over the place. I just learned about these pesky Whitespace Control characters that can get rid of the whitespace added from templating.
You can also strip whitespace in templates by hand. If you add a minus sign (-) to the start or end of a block (e.g. a For tag), a comment, or a variable expression, the whitespaces before or after that block will be removed:
{% for item in seq -%}
{{ item }}
{%- endfor %}
Note
This post is a thought [3]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://jinja.palletsprojects.com/en/3.0.x/templates/#whitespace-control
[2]: /html/
[3]: /thoughts/
Posts tagged: jinja
All posts with the tag "jinja"
4 posts
latest post 2023-12-26
Publishing rhythm
API — Jinja Documentation (3.1.x)
jinja.palletsprojects.com [1]
🤯 jinja comes with a loader to pre-compile templates! Defihnitely need to look at this for markata, as jinja is till one of the biggest hot spots.
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://jinja.palletsprojects.com/en/3.0.x/api/#jinja2.Environment.compile_templates
[2]: /thoughts/
[1]
I’ve definitely been missing out on setting up a proper jinja loader on a few projects, I need to lean on this a bit more.
class jinja2.FileSystemLoader(searchpath, encoding='utf-8', followlinks=False):
'''
Load templates from a directory in the file system.
'''
The path can be relative or absolute. Relative paths are relative to the current working directory.
loader = FileSystemLoader("templates")
# A list of paths can be given. The directories will be searched in order, stopping at the first matching template.
loader = FileSystemLoader(["/override/templates", "/default/templates"])
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: /static/https://jinja.palletsprojects.com/en/3.0.x/api/#jinja2.FileSystemLoader
[2]: /thoughts/
Template Designer Documentation — Jinja Documentation (3.1.x)
jinja.palletsprojects.com [1]
A feature of jinja that I just discovered is including sub templates. Here is an example from the docs.
{% include 'header.html' %}
Body goes here.
{% include 'footer.html' %}
And inside of my thoughts project I used it to render posts.
<ul id='posts'>
{% for post in posts.__root__ %}
{% include 'post_item.html' %}
{% endfor %}
</ul>
note that post_item.html [2] automatically inherits the post variable.
Note
This post is a thought [3]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://jinja.palletsprojects.com/en/3.1.x/templates/#include
[2]: /html/
[3]: /thoughts/