Sunday, January 8, 2017

Django - 018 - Template system basics

A Django template is a string of text that is intended to separate the presentation of a document from its data. A template defines placeholders and various bits of basic logic (template tags) that regulate how the document should be displayed. Usually, templates are used for producing, HTML, but Django templates are equally capable of generating any text-based format.

Philosophy behind Django templates
If you have a background in programming, or if you are used to languages which mix programming code directly into HTML, you will want to bear in mind that the Django template system is not simply Python embedded into HTML.

This is by design: the template system is meant to express presentation, not programming logic.

Let us start with a single example template. This Django template describes an HTML page that thanks a person for placing an order with a company. Think of it as a form letter:

<html>
<head> <title> Ordering notice </title> </head>
<body>
<h1> Ordering Notice </h1>
<p> Dear {{ person_name }},</p>
<p> Thanks for placing an order from {{ company }}. It is scheduled to ship on {{ ship_date|date:"F j, Y" }} . </p>
<p> Here are the item's you have ordered: </p>
<ul> 
{% for item in item_list %} <li> {{item}} <li> {% endfor %}
</ul>

{% if ordered_warrantly %}
 <p> Your warranty information will be included in the packaging.  </p>
{% else %}
 <p> You did not order a warranty, so you are on your own when the products inevitably stop working.</p>
{% endif%}
<p> Sincerely, <br/> {% company %} </p>

</body>
</html>
This template is basic HTML with some variables and template tags thrown in. Let us step through it:

Any text surrounded with a pair of braces (for example, {{person_name}} ) is a variable. This means "insert the value of the variable with the given name". How do we specify the values of the variables? We will get to that in a moment. Any text that is surrounded by curly braces and percent signs (for example, {% if ordered_warrantly %}) is a template tag. The definition of a tag is quite broad: a tag just tells the template system to "do something".
This example template contains a for tag ({% for item in item_list %}) and a if tag ({% if ordered_warrantly %}). A for tag works very much like a for statement in Python, letting you loop over each item in a sequence.
An if tag, as you may expect, acts as a logical if statement. In this particular case, the tag checks whether the value of the ordered_warranty variable evaluates to True. If it does, the template system will display everything between the {% if ordered_warrantly %} and {% else %} . If not, the template system will display everythin between the {% endif%} and  {% endif%}. Note that the {% else %}  is optional.
Finally, the second paragraph of this template contains an example of a filter, which is the most convenient way to alter the formatting of a variable. In this example, {{ ship_date|date:"F j, Y" }} , we are passing the ship_date variable to the date filter, giving the date filter the argument "F j, Y". The date filter formats dates in a given format, as specified by that argument. Filters are attached using a pipe character (|) , as a reference to Unix pipes.

Each Django template has access to several built-in tags and filters. It is a good idea to familiarize yourself with that list so you know what is possible. It is also possible to create your own filters and tags.



No comments:

Post a Comment