Задать вопрос

Тел: +7 965 3737 888

480

Просмотров

3

Ответов

Repeat blocks with new context / simple Jinja-like macro system

<p>A simple macro system that makes it possible to reuse previously defined 
blocks, optionally with a custom context, similar to the macro 
functionality in Jinja.  <br></p>
<p>It requires some workarounds/hacks because we cannot reach
all the data from inside the django template system that we need, but it
seems to work pretty well so far. It is, however, also pretty untested at this point, so use at your own risk.</p>
<p>Examples:</p>
base.html:

            &lt;!-- 
        This is mandatory if you want to use the repeat-tag in
        a template. It should as placed as earily as possible.
        See below for how to mix with template inheritance.
    --&gt;
            {% enablemacros %}

    &lt;!-- Note that {{ param }} does not exist. --&gt;

    {% block foo %}
        A standard django block that will be written to the output.
        {% if param %}{{ param }}{% endif %}
    {% endblock %}

    {% macro bar %}
        Pretty much the same thing as a django block (can even be 
        overridden via template inheritance), but it's content
        will NOT be rendered per default. Please note that it
        ends with ENDBLOCK!
        {% if param %}{{ param }}{% endif %}
    {% endblock %}

    &lt;!-- Render foo for the second time --&gt;
    {% repeat foo %} 
    &lt;!-- Render foo bar the first time --&gt;
    {% repeat bar %} 
    &lt;!-- Render both blocks again, and pass a parameter --&gt;
    {% repeat foo with "Hello World" as param %} 
    {% repeat bar with "Hello World" as param %}

    {% macro form %}do stuff with: {{ form }}{% endblock %}
    {% for form in all_forms %}
        {% repeat display %}  &lt;!-- will have access to {{ form }}
    {% endfor %}

extend.html:
    &lt;!--
        {% extends %} requires that it be the first thing in a template,
        and if it is, everything except for block tags is ignored, so
        {% enablemacros %} won't work. Instead, use:
    --&gt;
    {% extends_with_macros 'base.html' %}

    {% block foo %}
        Will override "foo" in base.html
    {% endblock %}
    {% block bar %}
        Will override the macro block "bar" in base.html. Whether
        this is defined as block or macro doesn't matter.
    {% endblock %}

<p>Todo:</p>
* This (both tags used) results in infinite recursion:            
    {% extends_with_macros "somefile" %}{% enablemacros %}

Вопрос полезен? Да0/Нет0
file_2425.py(7.3Кб)
None

Ответы (3):

Ответdiegor:11.12.2007
Ответ полезен? Да0/Нет0

{% load macro %} <- load the macro.py

{% enablemacros %}

{% macro bar %}

macro: {{ pm }}

{% endblock %}

{% repeat bar with pm as 'fooo' %}

{% repeat bar with pm as 'fooooo' %}

This print twice only "macro:" without 'fooo' or 'fooooo'. Any ideas about this reaction?

Ответmiracle2k:22.12.2007
Ответ полезен? Да0/Нет0

Sorry:

{% repeat bar with "fooooo" as pm %}

Ответmiracle2k:22.12.2007
Ответ полезен? Да0/Нет0

The docs on this were wrong, you have to use:

{% repeat bar with "fooooo" as bar %}