March 2020
Intermediate to advanced
608 pages
17h 17m
English
An advanced custom template tag consists of a function that parses the arguments that are passed to the tag, and a Node class that renders the output of the tag or modifies the template context. Perform the following steps to create the {% parse %} template tag:
# myproject/apps/core/templatetags/utility_tags.pyfrom django import templateregister = template.Library()""" TAGS """@register.tagdef parse(parser, token): """ Parses a value as a template and prints or saves to a variable Usage: {% parse <template_value> [as <variable>] %} Examples: {% parse object.description %} {% parse header as header %} {% parse "{{ MEDIA_URL }}js/" as ...