Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying jinja2 filters to a block?

Is it possible to apply jinja2 filters to {% block ... %} constructs? What I was hoping to do was something along the lines of:

{% block content|upper %}
here is some content that will be rendered in upper case
{% endblock %}

...but this doesn't work; the above example will result in an error. Is there any other way to wrap a chunk of template text in a jinja2 filter?

like image 729
larsks Avatar asked Oct 26 '12 20:10

larsks


People also ask

How do you use a Jinja filter?

Jinja2 filter is something we use to transform data held in variables. We apply filters by placing pipe symbol | after the variable followed by name of the filter. Filters can change the look and format of the source data, or even generate new data derived from the input values.

What is Jinja2 block?

Built into Jinja2 is functionality for building a 'base' template file and building off of it. This is known in Jinja2 functionality as “extending” a file. Files extend when they use the keyword block to define a section. The keyword endblock closes out a section.


1 Answers

You can use filter sections:

{% block content %}
    {% filter upper %}
        Here is some content that will be rendered in upper case.
    {% endfilter %}
{% endblock %}
like image 190
Eugene Naydenov Avatar answered Sep 27 '22 18:09

Eugene Naydenov