How it works...

The {% parse %} template tag allows you to parse a value as a template and render it immediately or save it as a context variable.

If we have an object with a description field, which can contain template variables or logic, we can parse and render it using the following code:

{% load utility_tags %}
{% parse object.description %}

It is also possible to define a value to parse using a quoted string, as shown in the following code:

{% load static utility_tags %}
{% get_static_prefix as STATIC_URL %} {% parse "{{ STATIC_URL }}site/img/" as img_path %} <img src="{{ img_path }}someimage.png" alt="" />

Let's take a look at the code of the {% parse %} template tag. The parsing function checks the arguments of the template tag bit by bit. At first, we expect the parse name and the template value. If there are still more bits in the token, we expect the combination of an optional as word followed by the context variable name. The template value and the optional variable name are passed to the ParseNode class.

The render() method of that class first resolves the value of the template variable and creates a template object out of it. The context vars are copied and a request context is generated, which the template is rendered. If the variable name is defined, the result is saved to it and an empty string is rendered; otherwise, the rendered template is shown immediately.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.172.252