Examples of custom template tags in Python's django framework

Source: Internet
Author: User
To customize a template tag, you need to tell Django how to do this when it encounters your tag.

When Django compiles a template, it divides the original template into nodes. Each node is an instance of Django.template.Node and has a render () method. Thus, a compiled template is a list of node objects. For example, look at this template:

Hello, {{person.name}}. {% ifequal name.birthday today%}  Happy birthday! {% Else%}  Be sure to come back on your birthday for  a splendid surprise message.{ % endifequal%}

The compiled template behaves as a list of nodes:

  • Text node: "Hello,"
  • Variable node: person.name
  • Text node: ". \ n"
  • Ifequal nodes: Name.birthday and today

When you invoke the render () method of a compiled template, the template invokes the render () method of each node on its node list with the given context. The results of these renderings are combined to form the output of the template. Therefore, to customize the template label, you need to indicate how the original template label is converted into a node (compile function) and the node's render () method completes the function.

In the following sections, we will explain in detail all the steps when writing a custom label.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.