Comparison with Django

If you have read through the Django Tutorial, you’ve seen examples for templating in Django. While the rest of Django, such as models, settings, migrations, etc., is the same with or without DMP, the way you do templates will obviously change with DMP. The following examples should help you understand the different between two template systems.

Note that, for the most part, the right column uses standard Python syntax. That’s why Mako rocks.
Django DMP (Mako)
Output the value of the question variable:
{{ question }}
${ question }
Call a method on the User object:
{{ user.get_full_name }}
${ user.get_full_name() }
Call a method with parameters on the User object:
{# Requires a custom tag #}
${ user.has_perm('app.get_main_view') }
Iterate through a relationship:
<ul>
    {% for choice in question.choice_set.all %}
        <li>{{ choice.choice_text }}</li>
    {% endfor %}
</ul>
<ul>
    %for choice in question.choice_set.all():
        <li>${ choice.choice_text }</li>
    %endfor
</ul>
Set a variable:
{% with name="Sam" %}
<% name = "Sam" %>
Format a date:
{{ value|date:"D d M Y" }}
${ value.strftime('%D %d %M %Y') }
Join a list:
{{ mylist | join:', ' }}
${ ', '.join(mylist) }
Use the /static prefix:
{% load static %}
<img src="{% get_static_prefix %}images/hi.jpg"/>
<img src="${ STATIC_ROOT }images/hi.jpg"/>
Call a Python method:
{% Requires a custom tag, unless a    %}
{% built-in tag provides the behavior %}
## Any Python method can be called:
<%! import random %>
${ random.randint(1, 10) }
Print a Django form:
<form action="/your-name/" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>
<form action="/your-name/" method="post">
    ${ csrf_input }
    ${ form }
    <input type="submit" value="Submit" />
</form>
Output a default if empty:
{{ value | default:"nothing" }}
## Use a boolean:
${ value or "nothing" }

## or use a Python if statement:
${ value if value is not None else "nothing" }
Run arbitrary Python:
{# Requires a custom tag  #}
## Keep it simple, Tex!
<%
    i = 1
    while i < 10:
        context.write('<p>Testing {0}</p>'.format(i))
    i += 1
%>
Inherit another template:
{% extends "base.html" %}
<%inherit file="base.htm" />
Override a block:
{% block title %}
    My amazing blog
{% endblock %}
<%block name="title">
    My amazing blog
</%block>
Link to a CSS file:
{# Place in template #}
<link rel="stylesheet" type="text/css" href="...">
## Automatically done by DMP (by name convention)
Perform per-request logic in JS files:
{# Difficult, young padwan...very difficult #}
## Wrap context keys with ``jscontext()``, and DMP will
## make the variable available in your JS file.