Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Replace Mako templating engine by Jinja2 and adjust templates accordi…

…ngly.
  • Loading branch information...
commit a3b7bfaffb4ca774c84b1f2c8ea623680da8263d 1 parent 2d4c9ae
@jmafc authored
View
4 wsgi/dbapp.py
@@ -43,10 +43,10 @@ def __init__(self, dbname):
self.handler = FilmHandler(self.dbconn)
def index(self):
- return render('/home.html')
+ return render('home.html')
def error404(self, msg=''):
- return render('/error/404.html', msg=str(msg))
+ return render('error/404.html', msg=str(msg))
def serve_css(self, path, resp):
text = None
View
20 wsgi/film.py
@@ -60,7 +60,7 @@ def db_error(self, exc):
def new(self):
"Displays a form to input a new film"
- return render('/film/new.html', id='')
+ return render('film/new.html', id='')
def create(self, **formdata):
"Saves the film data submitted from ``new``"
@@ -76,7 +76,7 @@ def create(self, **formdata):
else:
self.db.commit()
if errors:
- return render('/film/new.html', id=form.id, title=form.title,
+ return render('film/new.html', id=form.id, title=form.title,
release_year=form.release_year, errors=errors)
raise Redirect('/film/')
@@ -88,7 +88,7 @@ def index(self):
except Exception as exc:
film_list = []
errors = {None: self.db_error(exc)}
- return render('/film/list.html', films=film_list, errors=errors)
+ return render('film/list.html', films=film_list, errors=errors)
def edit(self, id):
"Displays a form for editing a film by id"
@@ -98,11 +98,11 @@ def edit(self, id):
try:
row = film.get(self.db)
except Exception as exc:
- return render('/film/edit.html', id=film.id,
+ return render('film/edit.html', id=film.id,
errors={None: self.db_error(exc)})
if not row:
raise NotFound("Film %d not found " % film.id)
- return render('/film/edit.html', id=film.id, title=film.title,
+ return render('film/edit.html', id=film.id, title=film.title,
release_year=film.release_year)
def save(self, **formdata):
@@ -119,7 +119,7 @@ def save(self, **formdata):
else:
self.db.commit()
if errors:
- return render('/film/edit.html', id=film.id, title=film.title,
+ return render('film/edit.html', id=film.id, title=film.title,
release_year=film.release_year, errors=errors)
raise Redirect('/film/')
@@ -131,11 +131,11 @@ def delete_conf(self, id=None):
try:
row = film.get(self.db)
except Exception as exc:
- return render('/film/edit.html', id=film.id,
+ return render('film/edit.html', id=film.id,
errors={None: self.db_error(exc)})
if not row:
raise NotFound("Film %d not found " % film.id)
- return render('/film/delete.html', id=film.id, film="%r" % (film))
+ return render('film/delete.html', id=film.id, film="%r" % (film))
def delete(self, id=None):
"Deletes an existing film by id"
@@ -145,14 +145,14 @@ def delete(self, id=None):
try:
row = film.get(self.db)
except Exception as exc:
- return render('/film/edit.html', id=film.id,
+ return render('film/edit.html', id=film.id,
errors={None: self.db_error(exc)})
if not row:
raise NotFound("Film %d not found " % film.id)
try:
film.delete(self.db)
except Exception as exc:
- return render('/film/delete.html', id=film.id, film="%r" % film,
+ return render('film/delete.html', id=film.id, film="%r" % film,
errors={None: self.db_error(exc)})
else:
self.db.commit()
View
13 wsgi/templates/base.html
@@ -1,12 +1,11 @@
-<%!
-appname = 'Movies'
-%>\
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
- ${next.page_title()}: ${self.attr.appname} Database Application
+ {% block page_title %}
+ {% endblock %}: {% block appname %}Movies Database Application
+ {%- endblock %}
</title>
<link rel="stylesheet" type="text/css" media="screen"
href="/static/dbapp.css" />
@@ -15,11 +14,11 @@
<body>
<div id="page">
<div id="header">
- <h1 id="appname">${self.attr.appname} Database Application</h1>
+ <h1 id="appname">{{ self.appname() }}</h1>
</div>
<div id="main">
- <h1>${next.page_title()}</h1>
- ${next.body()}
+ <h1>{{ self.page_title() }}</h1>
+ {% block content %}{% endblock %}
</div>
<div id="footer">
</div>
View
12 wsgi/templates/error/404.html
@@ -1,10 +1,12 @@
-<%inherit file="/base.html"/>
-<%def name="page_title()">Unknown Page</%def>\
+{% extends "base.html" %}
+{% block page_title %}Unknown Page{% endblock %}
+{% block content %}
<div id="content">
<h2>Not Found</h2>
<p>You have attempted to access a page that is <b>not available</b>
within this website.</p>
- % if msg:
- <p>Additional information: <b>${msg}</b></p>
- % endif
+ {% if msg -%}
+ <p>Additional information: <b>{{ msg }}</b></p>
+ {%- endif %}
</div>
+{% endblock %}
View
10 wsgi/templates/film/delete.html
@@ -1,9 +1,10 @@
-<%inherit file="/base.html"/>
-<%def name="page_title()">Are you sure?</%def>\
-<p>Are you sure you want to delete the film "${film}"?
+{% extends "base.html" %}
+{% block page_title %}Are you sure?{% endblock %}
+{% block content %}
+<p>Are you sure you want to delete the film "{{ film|e }}"?
All of the following related items will be deleted:</p>
<ul>
- <li>Film: <a href="/film/${id}">${film}</a>
+ <li>Film: <a href="/film/{{ id }}">{{ film|e }}</a>
</li>
</ul>
<form action="" method="post">
@@ -11,3 +12,4 @@
<input type="submit" value="Yes, I'm sure" />
</div>
</form>
+{% endblock %}
View
24 wsgi/templates/film/edit.html
@@ -1,19 +1,21 @@
-<%inherit file="/base.html"/>
-<%namespace file="fields.html" name="fields"/>
-<%def name="page_title()">Change Film</%def>\
+{% extends "base.html" %}
+{% block page_title %}Change Film{% endblock %}
+{% block content %}
<div id="content">
- % if errors:
+ {% if errors %}
<p class="errmsg">Please correct the following errors:</p>
- % if None in errors:
- <div class="errmsg">${errors[None]}</div>
- % endif
- % endif
- <form class="mainform" name="chgfilm" action="/film/save/${id}"
+ {% if None in errors %}
+ <div class="errmsg">{{ errors[None] }}</div>
+ {% endif %}
+ {% endif %}
+ <form class="mainform" name="chgfilm" action="/film/save/{{ id }}"
method="post">
- ${fields.body(edit=True)}
+ {%- set edit = true -%}
+ {% include "film/fields.html" %}
<div class="submit-btn">
- <p class="delbox"><a href="/film/delete/${id}">Delete</a></p>
+ <p class="delbox"><a href="/film/delete/{{ id }}">Delete</a></p>
<input type="submit" name="save" value="Save" />
</div>
</form>
</div>
+{% endblock %}
View
12 wsgi/templates/film/fields.html
@@ -1,9 +1,7 @@
-<%namespace name="form" file="/forms.html"/>
-<%page args="edit=False"/>
+{% import "forms.html" as form %}
<fieldset>
- <%form:text name="id" label="Id" size="10" value="${id}"
- readonly="${edit}"/>
- <%form:text name="title" label="Title" size="64" value="${title}"/>
- <%form:text name="release_year" label="Release Year" size="10"
- value="${release_year}"/>
+ {{ form.text('id', "Id", '10', value=id, readonly=edit, errors=errors) }}
+ {{ form.text('title', "Title", '64', value=title, errors=errors) }}
+ {{ form.text('release_year', "Release Year", '10', value=release_year,
+ errors=errors) }}
</fieldset>
View
28 wsgi/templates/film/list.html
@@ -1,27 +1,29 @@
-<%inherit file="/base.html"/>
-<%def name="page_title()">Select film to change</%def>\
+{% extends "base.html" %}
+{% block page_title %}Select film to change{% endblock %}
+{% block content %}
<ul class="newlink"><li><a href="/film/new">New film</a></li></ul>
<div id="changelist">
- % if errors:
+ {%- if errors %}
<p class="errmsg">Please correct the following errors:</p>
- % if None in errors:
- <div class="errmsg">${errors[None]}</div>
- % endif
- % endif
+ {% if None in errors %}
+ <div class="errmsg">{{ errors[None] }}</div>
+ {% endif %}
+ {% endif %}
<table border="1">
<thead>
<tr><th>Name</th></tr>
- % for i, film in enumerate(films):
- <tr class="row${(i % 2 + 1)}">
- <th scope="row"><a href="/film/${film.id}">${'%r' % (film)}</a></th>
- </tr>
- % endfor
</thead>
<tbody>
+ {% for film in films -%}
+ <tr class="{{ loop.cycle('row1', 'row2') }}">
+ <th scope="row"><a href="/film/{{ film.id }}">
+ {{- film|e }}</a></th>
</tr>
+ {% endfor -%}
</tbody>
</table>
<p class="paginate">
- ${len(films)} films
+ {{ films|length }} films
</p>
</div>
+{% endblock %}
View
19 wsgi/templates/film/new.html
@@ -1,18 +1,19 @@
-<%inherit file="/base.html"/>
-<%namespace file="fields.html" name="fields"/>
-<%def name="page_title()">New Film</%def>\
+{% extends "base.html" %}
+{% block page_title %}New Film{% endblock %}
+{% block content %}
<div id="content">
- % if errors:
+ {%- if errors %}
<p class="errmsg">Please correct the following errors:</p>
- % if None in errors:
- <div class="errmsg">${errors[None]}</div>
- % endif
- % endif
+ {%- if None in errors %}
+ <div class="errmsg">{{ errors[None] }}</div>
+ {% endif -%}
+ {% endif %}
<form class="mainform" name="addfilm" action="/film/create"
method="post">
- ${fields.body()}
+ {% include "film/fields.html" %}
<div class="submit-btn">
<input type="submit" name="save" value="Save" />
</div>
</form>
</div>
+{% endblock %}
View
88 wsgi/templates/forms.html
@@ -1,46 +1,52 @@
-<%def name="error(name)">\
-% if errors and name in errors:
- <div class="errmsg">${errors[name]}</div>
-% endif
-</%def>\
-<%def name="checkbox(name, label, value=None)">
+{# Individual field error message #}
+{% macro field_error(name, errors=None) %}
+{%- if errors and name in errors %}
+ <div class="errmsg">{{ errors[name] }}</div>
+{% endif -%}
+{% endmacro %}
+
+{# Checkbox #}
+{% macro checkbox(name, label, value=None, errors=None) %}
<div class="form-row">
-${error(name)}\
- <label for="${name}">${label}: </label>
- <input type="checkbox" id="${name}" name="${name}"\
-${value and ' checked="checked"' or ''} />
-</div>\
-</%def>\
-<%def name="select(name, label, iter, value=None)">
+ {{- field_error(name, errors) }}
+ <label for="{{ name }}">{{ label }}: </label>
+ <input type="checkbox" id="{{ name }}" name="{{ name }}"
+ {%- if value %} checked="checked"{% endif %} />
+</div>
+{%- endmacro %}
+
+{# Drop-down selector #}
+{% macro select(name, label, values, value=None, errors=None) %}
<div class="form-row">
-${error(name)}\
- <label for="${name}">${label}: </label>
- <select id="${name}" name="${name}">
- % for (k, v) in iter:
- % if value:
- <option value="${k}"\
-${k == value and ' selected="selected"' or ''}>${v}</option>
- % else:
- <option value="${k}">${v}</option>
- % endif
- % endfor
+ {{- field_error(name, errors) }}
+ <label for="{{ name }}">{{ label }}: </label>
+ <select id="{{ name }}" name="{{ name }}">
+ {% for key, val in values.items() -%}
+ <option value="{{ key }}"
+ {%- if value and key == value %} selected="selected"{% endif -%}>
+ {{- val }}</option>
+ {% endfor %}
</select>
-</div>\
-</%def>\
-<%def name="text(name, label, size, value=None, readonly=False)">
+</div>
+{%- endmacro %}
+
+{# Single-line text input #}
+{% macro text(name, label, size, value=None, readonly=False, errors=None) %}
<div class="form-row">
-${error(name)}\
- <label for="${name}">${label}: </label>
- <input type="text" id="${name}" name="${name}" size="${size}"\
-${value and (' value="%s"' % unicode(value).replace('"', '&quot;')) or ''}\
-${readonly and ' readonly="readonly"' or ''} />
-</div>\
-</%def>\
-<%def name="textarea(name, label, rows, cols, value=None)">
+ {{- field_error(name, errors) }}
+ <label for="{{ name }}">{{ label }}: </label>
+ <input type="text" id="{{ name }}" name="{{ name }}" size="{{ size }}"
+ {%- if value %} value="{{ value|e }}"{% endif %}
+ {%- if readonly %} readonly="readonly"{% endif %} />
+</div>
+{%- endmacro %}
+
+{# Multi-line text input #}
+{% macro textarea(name, label, rows, cols, value=None, errors=None) %}
<div class="form-row">
-${error(name)}\
- <label for="${name}">${label}: </label>
- <textarea id="${name}" name="${name}" rows="${rows}" cols="${cols}">\
-${value or ''}</textarea>
-</div>\
-</%def>\
+ {{- field_error(name, errors) }}
+ <label for="{{ name }}">{{ label }}: </label>
+ <textarea id="{{ name }}" name="{{ name }}" rows="{{ rows }}" cols="{{ cols }}">
+ {%- if value %}{{ value }}{% endif %}</textarea>
+</div>
+{%- endmacro %}
View
6 wsgi/templates/home.html
@@ -1,5 +1,6 @@
-<%inherit file="/base.html"/>
-<%def name="page_title()">Main Menu</%def>\
+{% extends "base.html" %}
+{% block page_title %}Main Menu{% endblock %}
+{% block content %}
<div id="section">
<table summary="Movies" class="admin-menu">
<caption>Movies</caption>
@@ -11,3 +12,4 @@
</tr>
</table>
</div>
+{% endblock %}
View
15 wsgi/templating.py
@@ -1,19 +1,14 @@
# -*- coding: utf-8 -*-
import os.path
-import tempfile
-from mako.lookup import TemplateLookup
+from jinja2 import Environment, FileSystemLoader
-lookup = TemplateLookup(
- directories=os.path.join(os.path.abspath(os.path.dirname(__file__)),
- 'templates'),
- module_directory=os.path.join(tempfile.gettempdir(), 'mako', 'dbapp'),
- filesystem_checks=True, collection_size=50,
- output_encoding='utf-8')
+env = Environment(loader=FileSystemLoader(os.path.join(
+ os.path.abspath(os.path.dirname(__file__)), 'templates')))
def render(filename, *args, **data):
- template = lookup.get_template(filename)
- return template.render(*args, **data)
+ template = env.get_template(filename)
+ return str(template.render(*args, **data))
Please sign in to comment.
Something went wrong with that request. Please try again.