Skip to content

Instantly share code, notes, and snippets.

@jesseyv
Created August 27, 2011 06:24
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jesseyv/1175064 to your computer and use it in GitHub Desktop.
Save jesseyv/1175064 to your computer and use it in GitHub Desktop.
Tornado Template Syntax
Basic Output
Hello, {{ current_user.name }}
Basic Logic
{% if not current_user %}
You're not logged in!
{% elif current_user and curent_user.is_admin %}
Good day, sir!
{% else %}
Howdy!
{% end %}
{% for link in links %}
<a href="{{ link.href }}">{{ link.title }}</a>
{% end %}
{% set i = 10 %}
{% while i %}
Item {{ i }}
{% set i -= 1 %}
{% end %}
{% try %}
{{ undeclared_var }}
{% except NameError, e %}
{{ e }}
{% end %}
{{ globals().get('undeclared_var') }}
Inheritance
<!-- base.html Template -->
<body>
<div id="main">
{% block main %}
You'll see me if the 'main' block isn't overridden
{% end %}
</div>
</body>
<!-- home.html Template -->
{% extends "base.html" %}
{% block main %}
Now you see me!
{% end %}
Comments
{% comment "I'm a comment" %}
Includes
{% include "other_template.html" %}
Imports
{% import os %}
{% import sys %}
Setting Variables
{% set DEBUG = True %}
{% set BASEPATH = os.getcwd() %}
Applying a function to a block
{% apply escape %}
<h1>Hello World</h1>
{% end %}
output: &lt;h1&gt;Hello World&lt;/h1&gt;
{% apply base64.b64encode %}
Base64 Encode Me!
{% end %}
output: CkJhc2U2NCBFbmNvZGUgTWUhCg==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment