Render content fields with Jinja2.
Project description
lektor-jinja-content
This is a Lektor plugin that allows you to render Jinja2 inside your content fields that are string-like. For example, this means that inside your content fields that are of type string or Markdown, you can write Jinja logic, and access Lektor's Template Context, and has access to all normal Jinja filters, including those provided by other plugins. In a Markdown field (or other field that is rendered - like rst), the Jinja is processed first, then the formatting processor.
You can set and use Jinja variables, but they will only have meaning within their field that is being rendered.
With this plugin, you'll have to make sure the content that is rendered is valid Jinja. Jinja syntax erros will throw an exception. Don't forget about the handy {% raw %} {% endraw %}
tags if you want content that is not valid Jinja.
N.B. Using this plugin is rendering many more items with Jinja, and your build process will slow down as a result. As an early benchmark and anecdote, my small website's build time rose from 4.89s to 5.39s even while I actually had no Jinja-Content that needed rendering. I plan on adding the ability to configure this plugin later so that you don't have to run everything through Jinja if you don't want to.
Examples
Querying context: {{ site.get('/').title }}
or {{ this.path }}
Logic:
{% set meaning_of_life, meaning_of_universe = this.life, this.universe %}
{% if meaning_of_life == meaning_of_universe == 42 %}
{% set meaning_of_it_all = meaning_of_life %}
{% else %}
{% set meaning_of_it_all = 'Undefined' %}
{% endif %}
{{ meaning_of_it_all }}
Jinja in Markdown:
[link text]({{ this|url }})
Jinja in reStructuredText:
`link text <{{ this|url }}>`_
Try running the test site for more examples.
Possible future of this plugin.
This Plugin opens the door to some pretty powerful and pretty funky functionality. Here's some food for though:
- Currently every every string-like field in
_data
and_blocks
is processed, even things like_slug
and_template
. Can these other things be used? - Some people want to be able to include markdown in content files / declarations. This plugin might pave the way toward that with includes and extends.
- Could we Jinjaify static files?
- Databags?
- What about modularity?
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file lektor-jinja-content-0.4.1.tar.gz
.
File metadata
- Download URL: lektor-jinja-content-0.4.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e3ae3ca35c145f7d81f4833b62f1108acfef8d6a9410e3612708f65321c19a4 |
|
MD5 | d5e2d3fad71cee55d868a73ac8821bab |
|
BLAKE2b-256 | 7487084e96cb4c4f5d55fac59a54c45f21ccea402ce7c99bf235c160990dfb42 |
Provenance
File details
Details for the file lektor_jinja_content-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: lektor_jinja_content-0.4.1-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd4e12117f246a95c072f14f4bded2f59ea7e142f523da7d0f5ae87628d54520 |
|
MD5 | 91799eea2ac35d247952a6ee2671e502 |
|
BLAKE2b-256 | 5c6948106d7daae18f1a711bc2947ba3fc4c834eb649fee6408ca1c9375c7eb9 |