Templating

Yes, this article is still about Django. In this short article, I will explain about templating. Django uses a templating language that differs from Python itself. Why? Because, this language is intended for web designers. Thus, web designers don’t have to understand “real programming” with Python. And also this is also useful to make clear difference between application logic and user interface. With this simple templating language, Django can prevent developers from using too much programming logic in a template.

Things you should remember about this templating language are:

  1. The language is PURELY for displaying data
  2. You can’t declare variables or functions in a template
  3. Operators are basic and limited
  4. Only support basic programming logic like looping or operator
  5. The language consists of template tags, template filters,  and context variables
  6. Template tags are in {% %} bracket. Examples: {% for %}, {% endfor %}
  7. Template filters are for simple data processing, like formatting or basic computation. Filters are identified by “|”. Examples: users|last, users|element:5
  8. You can define custom template tags and template filters
  9. Variables in a template are called context variables
  10. You can define context processors in each Django app. Context processors are like “pre-loaded” context variables that will be loaded every time a template being rendered. This is perfect for displaying data that must be displayed on every page. Examples: is_logged, username

Another interesting thing about templating in Django is: template inheritance. Template inheritance is powerful for layouting your template. You can define a basic layout containing header, content, and footer called layout.html. Then you can create a template that extends layout.html, with different content than another template which is also extending layout.html. In short, those two templates have same layout, but different content. Thus, you don’t have to write header & footer on every template.

For example, this is the layout.html


<html>

<head><title>Title</title></head>

<body>

    {% block header %}

        This is header

    {% endblock %}

    <div class="content">

         {% block content %}

         {% endblock %}

    </div>

    {% block footer %}

        This is footer

    {% endblock %}

</body>

</html>

And this is a template that extends layout.html (page1.html):


{% extends "layout.html" %}

{% block content %}

    This is content of page 1

{% endblock %}

So, page1.html will display a complete web page with “This is content of page 1” as the content.

Simple and pretty neat :p

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s