Skip to content

Instantly share code, notes, and snippets.

@jdcaballerov
Created April 1, 2015 03:22
Show Gist options
  • Save jdcaballerov/436852d0b3a142fe4d08 to your computer and use it in GitHub Desktop.
Save jdcaballerov/436852d0b3a142fe4d08 to your computer and use it in GitHub Desktop.

Comparison with Django Syntax

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 standard Django and DMP template syntax.

Note in the examples how the DMP column normally uses standard Python syntax, with no extra language to learn:

  • Output the value of the question variable:
Django Templates DMP (Mako) Templates
{{ question }}
${ question }
- Output a user's full name (a method on User):
Django Templates DMP (Mako) Templates
{{ user.get_full_name }}
${ user.get_full_name() }
- Iterate through a relationship:
Django Templates DMP (Mako) Templates
<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:
Django Templates DMP (Mako) Templates
{% with name="Sam" %}
<% name = "Sam" %>
- Format a date:
Django Templates DMP (Mako) Templates
{{ value|date:"D d M Y" }}
${ value.strftime('%D %d %M %Y') }
- Join a list:
Django Templates DMP (Mako) Templates
{{ mylist | join:', ' }}
${ ', '.join(mylist) }
- Use the /static prefix:
Django Templates DMP (Mako) Templates
{% load static %}
<img src="{% get_static_prefix %}images/hi.jpg"/>
<img src="${ settings.STATIC_ROOT }images/hi.jpg"/>
- Call a Python method:
Django Templates DMP (Mako) Templates
Requires a custom tag, unless a built-in tag provides the behavior. Any Python method can be called:
<%! import random %>
${ random.randint(1, 10) }
- Output a default if empty:
Django Templates DMP (Mako) Templates
{{ value | default:"nothing" }}
Use a boolean:
${ value or "nothing" }
or use a Python if statement:
${ value if value != None else "nothing" }
- Run arbitrary Python (keep it simple, Tex!):
Django Templates DMP (Mako) Templates
Requires a custom tag
<%
  i = 1
  while i < 10:
    context.write('<p>Testing {0}</p>'.format(i))
    i += 1
%>
- Inherit another template:
Django Templates DMP (Mako) Templates
{% extends "base.html" %}
<%inherit file="base.htm" />
- Override a block:
Django Templates DMP (Mako) Templates
{% block title %}My amazing blog{% endblock %}
<%block name="title">My amazing blog</%block>
- Link to a CSS file:
Django Templates DMP (Mako) Templates
Place in each template:
<link rel="stylesheet" type="text/css" href="...">
Simply name the .css/.js file the same name as your .html template. DMP will include the link automatically.
- Perform per-request logic in CSS or JS files:
Django Templates DMP (Mako) Templates
Create an entry in ``urls.py``, create a view, and render a template for the CSS or JS. Simply name the .css file as name.cssm for each name.html template. DMP will render the template and include it automatically.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment