4

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.

1 Answer 1

3

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()) }}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.