用Jinja2中的模板输出替换字符串

时间:2016-01-08 14:42:46

标签: jinja2 pelican

Pelican 中,页面的内容将作为mode:"MultiSelect"传递到 Jinja2 模板。我们假设page.content包含字符串page.content。我现在怎么能用一些模板逻辑的结果替换这个字符串{REPLACEME},比如说

{REPLACEME}

我的想法是,我想使用模板逻辑来呈现项目列表,但仍然能够决定此列表在{% 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 %} 中的显示位置。

我知道 Jinja2 中有page.content filter,但我无法弄清楚如何使replace()参数包含上面的模板ouptut

1 个答案:

答案 0 :(得分:3)

原来在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 %}

然后我可以在调用replace时使用定义的宏:

    {{ page.content | replace("{PUBLICATIONS}", bibtex_rendered()) }}
相关问题