In Pelican, a page's content is passed to the Jinja2 template as page.content
. Let's assume that page.content
contains a string {REPLACEME}
. How can I now replace this string {REPLACEME}
with the result from some template logic, say
{% for pub in publications %}
{% set key, year, text, bibtex, doi, url, pdf, slides, poster = pub %}
{% if "%s"|format(year) == "%s"|format(yr) %}
<li id="{{ key }}">{{ text }}</li>
{% endif %}
{% endfor %}
The idea is that I want to use template logic to render a list of items, but still be able to decide where this list should appear within the page.content
.
I know that there is a replace()
filter in Jinja2, but I cannot figure out how to make the new
argument contain the template ouptut from above.
Turns out there's macros in Jinja2.
{% macro bibtex_rendered() -%}
{% for pub in publications %}
{% set key, year, text, bibtex, doi, url, pdf, slides, poster = pub %}
{% if "%s"|format(year) == "%s"|format(yr) %}
<li id="{{ key }}">{{ text }}</li>
{% endif %}
{% endfor %}
{%- endmacro %}
Then I can use the defined macro in my call to replace
:
{{ page.content | replace("{PUBLICATIONS}", bibtex_rendered()) }}