templayer-1.5.1/setup.py 0000644 0001750 0001750 00000000533 10505761642 013341 0 ustar ian ian
from distutils.core import setup
import templayer
version = templayer.__version__
setup (
name = "templayer",
version = version,
description = "Templayer - Layered Template Library for HTML",
author = "Ian Ward",
author_email = "ian@excess.org",
url = "http://excess.org/templayer/",
license = "LGPL",
py_modules = ['templayer'],
)
templayer-1.5.1/templayer.py 0000644 0001750 0001750 00000071556 11150403641 014205 0 ustar ian ian #!/usr/bin/python
"""
templayer.py - Layered Template Library for HTML
http://excess.org/templayer/
library to build dynamic html that provides clean separation of form
(html+css+javascript etc..) and function (python code) as well as
making cross-site scripting attacks and improperly generated html
very difficult.
Copyright (c) 2003-2009 Ian Ward
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301, USA.
"""
__author__ = "Ian Ward"
__version__ = "1.5.1"
import string
import sys
import os
import time
import re
from types import TupleType, ListType, IntType, UnicodeType
from inspect import getargspec
try: True # old python?
except: False, True = 0, 1
SLOT_MARK = "%"
BEGIN_MARK = "{"
END_MARK = "}"
CLOSE_PREFIX = "/"
CONTENTS = "contents"
CONTENTS_OPEN = BEGIN_MARK+CONTENTS+END_MARK
CONTENTS_CLOSE = BEGIN_MARK+CLOSE_PREFIX+CONTENTS+END_MARK
COMPLETE_FILE_LAYER = "*"
ENTITY_RE = re.compile("^#x[0-9A-Fa-f]+$|^#[0-9]+$|^[a-zA-Z]+$")
TEMPLAYER_PAGES_MODULE = "templayer_pages"
class TemplateError(Exception):
pass
class Template(object):
"""
class Template - common functionality for Template subclasses
"""
def __init__( self, name, from_string=False, auto_reload='never',
allow_degenerate=False, encoding='utf_8'):
"""
name -- template file name or file-like object
from_string -- if True treat name as a string containing the
template data instead of a file to read from
auto_reload -- if the template has not been checked in this
many seconds then check it and reload if there
are any changes. If 'never' only load the
template file once.
Checking and reloading is done by the
start_file() function.
Ignored if from_string is True or if name is a
file-like object.
allow_degenerate -- If True then template is allowed to not
have a {contents}...{/contents} block. In this
case the entire file is available as a layer
called '*'. eg. l = tmpl.format('*', ...)
encoding -- encoding for template file
"""
self.allow_degenerate = allow_degenerate
self.encoding = encoding
self.auto_reload = auto_reload
self.template_ts = 0
self.template_name = None
if from_string:
data = name
if type(data)!=UnicodeType:
data = data.decode(encoding)
self.set_template_data(data)
auto_reload = None
elif hasattr(name, "read"):
self.set_template_data(name.read().decode(encoding))
auto_reload = 'never'
else:
self.template_name = name
self.reload_template()
def reload_template(self):
"""Reload the template data from the template file."""
if not self.template_name: return
tmpl = open(self.template_name).read().decode(self.encoding)
self.set_template_data(tmpl)
def template_file_changed(self):
"""
Return True if the template file has been modified since
the last time it was loaded.
"""
if self.template_ts == "never":
return False
if not self.template_name:
return True # we don't know if it has changed
if os.stat(self.template_name)[8] != self.template_ts:
return True
def set_template_data(self,tmpl):
"""Parse and store the template data passed."""
if type(tmpl) != UnicodeType:
raise TemplateError("template data must be "
"a unicode string")
self.cache = {}
try:
# use the first CONTENTS_BEGIN in the file
header, rest = tmpl.split(CONTENTS_OPEN,1)
# and the last CONTENTS_CLOSE in the file
rest_l = rest.split(CONTENTS_CLOSE)
if len(rest_l)<2:
raise ValueError
footer = rest_l[-1]
body = CONTENTS_CLOSE.join(rest_l[:-1])
self.contents_open = CONTENTS_OPEN
self.contents_close = CONTENTS_CLOSE
except ValueError:
if not self.allow_degenerate:
raise TemplateError, "Template Contents Block "\
"Missing: %s...%s" % \
(CONTENTS_OPEN, CONTENTS_CLOSE )
# degenerate template
header = tmpl
footer = body = ""
self.contents_open = self.contents_close = ""
self.header = header.split(SLOT_MARK)
self.footer = footer.split(SLOT_MARK)
self.body = []
body_l = body.split(BEGIN_MARK)
for b in body_l:
self.body.append( b.split(SLOT_MARK) )
def layer_split( self, name ):
"""Return the layer named name split on SLOT_MARKs."""
if BEGIN_MARK in name or SLOT_MARK in name:
raise TemplateError, "Layer names cannot include %s " \
"or %s" % (BEGIN_MARK, SLOT_MARK)
if name == COMPLETE_FILE_LAYER:
begin = 0
end = len(self.body)
elif self.cache.has_key(name):
begin, end = self.cache[name]
else:
begin = None
end = None
i = 1
for b in self.body[1:]:
if begin is None \
and b[0].startswith(name+END_MARK):
begin = i
if begin is not None and b[0].startswith(
CLOSE_PREFIX+name+END_MARK):
end = i
i += 1
if end is None or begin is None:
raise TemplateError, "Template Layer Missing:"\
" %s...%s"%(BEGIN_MARK+name+END_MARK,
BEGIN_MARK+CLOSE_PREFIX+name+END_MARK)
self.cache[name] = begin, end
o = []
for i in range(begin, end):
b = self.body[i]
if o:
# join across BEGIN_MARKs
o[-1] = o[-1]+BEGIN_MARK+b[0]
o.extend(b[1:])
else:
o.extend(b)
if name == COMPLETE_FILE_LAYER:
# include the header and footer
c = self.header[:-1]
c.append(self.header[-1] + self.contents_open + o[0])
c.extend(o[1:])
c[-1] = c[-1] + self.contents_close + self.footer[0]
c.extend(self.footer[1:])
o = c
else:
# remove the opening layer tag
o[0] = o[0][len(name)+len(END_MARK):]
return o
def layer( self, name ):
"""Return the complete layer named name as a string."""
return SLOT_MARK.join(self.layer_split(name))
def missing_slot(self, names, layer):
"""Called when one or more slots are not found in a layer."""
if layer is None:
where = "Header/Footer"
else:
where = "Layer %s" % (BEGIN_MARK+layer+END_MARK)
slots = [SLOT_MARK+name+SLOT_MARK for name in names]
if len(slots)>1:
slotp = "slots"
else:
slotp = "slot"
raise TemplateError, "%s is missing the following %s: %s" % (
where, slotp, ", ".join(slots))
def format_header_footer( self, **args ):
"""Returns header and footer with slots filled by args."""
fargs = {}
for k, v in args.items():
k, v = self.pre_process(k, v)
fargs[k] = v
header, missing_h = fill_slots(self.header, **fargs)
footer, missing_f = fill_slots(self.footer, **fargs)
d = {}
for m in missing_h:
d[m] = True
missing = [m for m in missing_f if d.has_key(m)]
if missing:
self.missing_slot(missing, None)
return self.post_process(header), self.post_process(footer)
def format( self, layer_name, ** args ):
"""
Return a layer with slots filled by args.
self.format(...) and self(...) are equivalent.
"""
s = self.layer_split(layer_name)
fargs = {}
for k, v in args.items():
k, v = self.pre_process(k, v)
fargs[k] = v
s, missing = fill_slots(s, **fargs)
if missing:
self.missing_slot(missing, layer_name)
return self.post_process(s)
__call__ = format
def start_file( self, file=None, encoding='utf_8' ):
"""
Return a FileWriter object that uses this template.
file -- file object or None to use sys.stdout.
If self.auto_reload is not None this function will first
check and reload the template file if it has changed.
"""
if self.auto_reload is not None:
t = time.time()
if self.auto_reload != "never" and \
t > self.template_ts+self.auto_reload:
if self.template_file_changed():
self.reload_template()
self.template_ts = t
return FileWriter( file, self, encoding )
def pre_process( self, key, value ):
"""
Returns key, filtered/escaped value.
Override this function to provide escaping or filtering of
text sent from the module user.
"""
return key, value
def post_process( self, value ):
"""
Returns wrapped value.
Override to wrap processed text in order to mark it as
having been processed.
"""
return value
def finalize( self, value ):
"""
Return unwrapped value as a string.
Override this function to reverse wrapping applied before
sending to the output file.
"""
return value
def fill_slots(layer_split, **args):
"""Return layer_split filled with args, missing slot list."""
filled = {}
o = []
last_is_slot = True
for p in layer_split[:-1]:
if last_is_slot:
last_is_slot = False
o.append(p)
elif args.has_key(p):
o.append(unicode(args[p]))
filled[p] = True
last_is_slot = True
else:
o.extend([SLOT_MARK,p])
if not last_is_slot:
o.append(SLOT_MARK)
o.append(layer_split[-1])
missing = []
if len(filled) content "+expand_html_markup(v[1])+" We've got a groundhog. I will have to stay alert. I lost half a tomato plant to that furry guy. The grass grew - I saw it. We've got a groundhog. I will have to stay alert. I lost half a tomato plant to that furry guy. The grass grew - I saw it. We've got a groundhog. I will have to stay alert. I lost half a tomato plant to that furry guy. The grass grew - I saw it. Generated on %date%. Generated on %date%. %what% Generated on %date%. %contents% Generated on %date%.
{toc_section}
{toc_item}
{section_head}
Our examples will be based on a single-page web site:
All the example source code may be extracted from the docgen_tutorial.py
script that is included with Templayer by running:
* count"""
return expand_html_markup(('br', count))
def P(content=""):
"""HTML Markup Gordon's Lawn Happenings
Sunday
Monday
Gordon's Lawn Happenings
{contents}
{/contents}
Sunday
Monday
%title%
{contents}
{/contents}
Sunday
Monday
%title%
{contents}
{report}
%day%
%happenings%
{/report}
{/contents}
%title%
{contents}
{report}
%day%
%happenings%
{/report}
{happening}
%title%
{contents}
{report}
%day%
%contents%
{/report}
{happening}
%title%
{contents}
{/contents}
"""
examples["simple.templayer"] = ["example_simple_views_1", "example_simple_views_2"]
def example_simple_views_1():
from django.http import HttpResponse
import datetime
import templayer
tmpl = templayer.get_django_template("simple.templayer.html")
def current_datetime(request):
file_writer = tmpl.start_file(HttpResponse())
now = datetime.datetime.now()
contents = file_writer.open(title="Current Date and Time")
contents.write("It is now %s." % now)
return file_writer.close()
def example_simple_views_2():
import datetime
import templayer
tmpl = templayer.get_django_template("simple.templayer.html")
@templayer.django_view(tmpl)
def current_datetime(file_writer, request):
now = datetime.datetime.now()
contents = file_writer.open(title="Current Date and Time")
contents.write("It is now %s." % now)
template["book.templayer"] = """
%title%
{contents}
{new_entry}
{/new_entry}
{/contents}
"""
examples["book.templayer"] = ["example_book_models", "example_book_views"]
def example_book_models():
from django.db import models
RATING_CHOICES = (
('G', "Great!"),
('A', "Average"),
('U', "Uninteresting"),
)
class Entry(models.Model):
name = models.CharField("Your Name", max_length=100)
rating = models.CharField("Rate this site", max_length=1,
choices=RATING_CHOICES)
comment = models.TextField("Your Comments (optional)", blank=True)
posted = models.DateTimeField(auto_now_add=True)
def example_book_views():
from book.models import Entry
from django.forms import ModelForm
import templayer
tmpl = templayer.get_django_template("book.templayer.html")
class EntryForm(ModelForm):
class Meta:
model = Entry
@templayer.django_view(tmpl)
def guest_book(file_layer, request):
if request.POST:
entry_form = EntryForm(request.POST)
# TODO: do something with the data if all is well
else:
entry_form = EntryForm()
contents = file_writer.open(title="Guest Book")
contents.write_layer("new_entry", **templayer.django_form(entry_form))
template["emulate.templayer"] = """
%title%
{contents}
{show_404}
The url %url% could not be found.
{/show_404}
{flatpage_body}
%contents%
{/flatpage_body}
{/contents}
"""
examples["emulate.templayer"] = ["example_templayer_pages"]
def example_templayer_pages():
import templayer
tmpl = templayer.get_django_template("emulate.templayer.html")
@templayer.django_template(tmpl, "404.html")
def show_404(file_writer, context, request_path):
contents = file_writer.open(title="Page not found")
# request_path == context['request_path']
contents.write_layer("show_404", url=request_path)
@templayer.django_template(tmpl, "flatpages/default.html")
def show_flatpage(file_writer, context, flatpage):
contents = file_writer.open(title=flatpage.title)
fp_contents = contents.open_layer("flatpage_body")
# the flatpage content is HTML, so don't escape it.
fp_contents.write(templayer.RawHTML(flatpage.content))
def read_sections(tmpl):
"""Read section tags, section descriptions, and column breaks from
the Templayer template argument. Convert the section data into a
Python data structure called sections. Each sublist of sections
contains one column. Each column contains a list of (tag, desc.)
pairs. Return sections."""
sd = tmpl.layer("section_data")
col_break = "---"
sections = [[]]
for ln in sd.split("\n"):
if not ln: continue
if ln == col_break:
sections.append([])
continue
tag, desc = ln.split("\t",1)
sections[-1].append( (tag, templayer.RawHTML(desc)) )
return sections
def read_example_code():
"""By the time this function runs, the examples dictionary contains
a list of function names, all starting with "example_". Create a
second dictionary called code_blocks. Open the file containing this
function. For each function name in examples, read the text of that
function into an entry in the code_blocks dictionary. Return the
code_blocks dictionary."""
# invert the "examples" dictionary
example_fns = {}
for tag, l in examples.items():
for i, fn in zip(range(len(l)), l):
example_fns[fn] = tag, i
# read our own source code
# strip trailing spaces and tabs from each line
code_blocks = {}
current_block = None
for ln in open( sys.argv[0], 'r').readlines():
ln = ln.rstrip()
if ( ln[:4] == "def " and ln[-3:] == "():" and
example_fns.has_key( ln[4:-3] ) ):
current_block = ln[4:-3]
code_blocks[current_block] = []
continue
if ln and ln[:1] != "\t":
current_block = None
continue
if current_block is None:
continue
if ln[:1] == "\t":
ln = ln[1:]
code_blocks[current_block].append(ln+"\n")
# recombine code lines into a single string each
for name, block in code_blocks.items():
code_blocks[name] = "".join( block )
return code_blocks
def write_example_files(sections, blocks):
for t in template.keys():
for e in examples.get(t, []):
assert e.startswith("example_")
open(e[8:]+".py", "w").write(blocks[e])
open(t+".html", "w").write(template[t].lstrip())
class SimulatedOutput(object):
output = []
def write(self, s):
self.output.append(s)
def flush(self):
pass
class NullOutput(object):
def write(self, s):
pass
def get_simulated_output():
s = "".join(SimulatedOutput.output)
del SimulatedOutput.output[:]
return s
class SimulatedHTMLTemplate(templayer.HTMLTemplate):
def __init__(self, name):
name = name[:-len(".html")]
t = template[name]
head, t = t.split("",1)
t, tail = t.split("",1)
t = t.strip()
self.encoding = "utf_8"
self.set_template_data(template[name].decode(self.encoding))
def start_file(self, file=None):
return templayer.FileLayer(SimulatedOutput(), self)
def generate_results():
"""
Capture the output from the examples and return a dictionary
containing one result for each section that needs one.
"""
def get_result(fn):
# divert the output to our simulated version
orig_tmpl = templayer.HTMLTemplate
orig_stdout = sys.stdout
templayer.HTMLTemplate = SimulatedHTMLTemplate
sys.stdout = NullOutput()
fn()
# restore the original values
templayer.HTMLTemplate = orig_tmpl
sys.stdout = orig_stdout
return get_simulated_output()
r = {}
r['lawn1'] = [get_result(example_lawn2)]
r['lawn3'] = [get_result(example_lawn3)]
r['lawn4'] = [get_result(example_lawn4b), get_result(example_lawn4c)]
return r
def generate_body(tmpl, sections, blocks):
# put TOC columns into the variables used by the template
# assign section numbers
# generate HTML form of TOC entries, corresponding document parts
assert len(sections) == 2, 'sections has %d columns but should have 2!' % len(sections)
toc_slots = {'toc_left':[], 'toc_right':[]}
body = []
results = generate_results()
snum = inum = 0
for slot, l in zip(['toc_left','toc_right'], sections):
for tag, name in l:
if not tag:
# new section -- do its first item next time
snum += 1
inum = 0
t = tmpl.format('toc_section', snum=`snum`,
name=name )
toc_slots[slot].append( t )
b = tmpl.format('section_head', snum=`snum`,
name=name )
body.append( b )
continue
# new item inside a section
inum += 1
t = tmpl.format('toc_item', snum=`snum`,
inum=`inum`, name=name, tag=tag)
toc_slots[slot].append( t )
slots = {}
slots['html'] = tmpl.format('html_example',
name = tag+".html",
contents = template[tag])
i = 0
for fn in examples.get(tag, []):
c = tmpl.format('code_example',
name = fn[len("example_"):]+".py",
contents = blocks[fn])
slots['code[%d]'%i] = c
i += 1
i = 0
for r in results.get(tag, []):
c = tmpl.format('result',
contents = templayer.RawHTML(r))
slots['result[%d]'%i] = c
i += 1
b = tmpl.format('body[%s]'%tag, ** slots )
b = tmpl.format('section_body', snum=`snum`,
inum=`inum`, name=name, tag=tag,
contents = b)
body.append( b )
return (body, toc_slots)
def parse_options():
usage = "%s [-h|-?|--help]\n%s [-H|--HTML|--html] [-s|--scripts]" % \
(sys.argv[0], sys.argv[0])
help = """%s options:
-h, -?, --help Print this message to standard error and exit.
-H, --HTML, --html Write the HTML documentation to standard output.
-s, --scripts Write runnable scripts to files.""" % sys.argv[0]
do_html = False
do_scripts = False
if len(sys.argv) < 2 or len(sys.argv) > 3:
sys.exit(usage)
if len(sys.argv) == 2 and (sys.argv[1] in ('-h', '-?', '--help')):
sys.exit(help)
for arg in sys.argv[1:]:
if arg in ('-H', '--HTML', '--html'):
if do_html: sys.exit(usage)
else: do_html = True
elif arg in ('-s', '--scripts'):
if do_scripts: sys.exit(usage)
else: do_scripts = True
else:
sys.exit(usage)
return (do_html, do_scripts)
def main():
(do_html, do_scripts) = parse_options()
tmpl = templayer.HTMLTemplate(
os.path.join(os.path.dirname(sys.argv[0]),
"tmpl_tutorial.html"))
sections = read_sections( tmpl )
code_blocks = read_example_code()
if do_scripts:
write_example_files( sections, code_blocks )
if do_html:
out_file = tmpl.start_file()
(body, toc_slots) = generate_body( tmpl, sections,
code_blocks )
bottom = out_file.open(version=templayer.__version__,
** toc_slots)
bottom.write( body )
out_file.close()
if __name__=="__main__":
main()
templayer-1.5.1/tmpl_tutorial.html 0000644 0001750 0001750 00000037123 11150315456 015414 0 ustar ian ian
Templayer %version% Tutorial
{contents}
%toc_left%
%toc_right%
Templayer Tutorial Template File
This file is used by docgen_tutorial.py to generate the tutorial
documentation tutorial.html.
Items in the list that follows are parsed by docgen_tutorial.py.
Each item has a tag and a name, separated by a tab character.
Items without tags are new sections.
A --- separates the left and right columns in the table of contents.
Tag Section or Item Name
{section_data}
CGI Script: Gordon's Lawn Happenings
lawn1 Static HTML (before using Templayer)
lawn2 Minimal Templayer Template
lawn3 Using Slots
lawn4 Using a Simple Layer
lawn5 Using Nested Layers
lawn6 Advanced Nested Layers
---
Django Applications and Templayer
simple.templayer Simple Views
book.templayer Forms
emulate.templayer Emulating Django Templates
{/section_data}
%snum%. %name%
{/section_head}
{section_body}
%snum%.%inum%. %name%
[back to top]
%contents%
{/section_body}
{html_example}
%contents%
{code_example}
%contents%
{result}
{body[lawn1]}
python docgen_tutorial.py -s
The first thing we need to do to use this page as a Templayer template file is to add "{contents}" and "{/contents}" labels to the file:
%html%Templayer searches the template file for the first occurrence of "{contents}" and the last occurrence of "{/contents}". This splits the file into header, contents and footer.
The header and footer will appear in Templayer's output. The contents is described in sections below.
%code[0]%This is a minimal CGI script that uses Templayer to display the web site. The result of running this script will be the same as if we just used the static page above.
We first create an HTMLTemplate object to handle parsing of the template above.
The start_file function creates a FileWriter object that will use the our HTMLTemplate object. The FileWriter object handles a single output run for our template. The start_file function allows you to pass in a file-like object to use instead of the standard output. For a CGI script we use the default.
The FileWriter object's open function will be explained below.
No output is written until the close function is called at the end of the script. If you don't get any output from your script it might be because you forgot to call the close function on your FileWriter object.
{/body[lawn2]}The next step is to add slots to the header and footer of the template. Slots are like variables that can be filled in by the application. In the template file slot names are wrapped in "%" characters:
%html%We have added two %title% slots to the header and one %date% slot to the footer of this template.
Please note: "%" is not a special character in Templayer templates. You do not need to escape "%" characters that aren't part of a slot. When filling slots Templayer has a list of slot names and it only replaces those slots, leaving all the other "%" characters in the template intact.
%code[0]%Essentially only one line has changed in the code. The open function takes keyword parameters where the names of the parameters correspond to slots in the header and footer. Here both %title% slots will be filled with "Gordon's Lawn Happenings", and the %date% slot will be filled with the current date and time.
The values passed in to fill slots are interpreted by our HTMLTemplate object. The HTMLTemplate object expects HTML Markup described by the expand_html_markup function. Plain strings are treated as unsafe in HTML Markup and any characters that might be interpreted as HTML are escaped by this function. This means that Templayer's default behaviour is to escape values being filled into HTML templates.
%result[0]% {/body[lawn3]}Now we look at what we can do with the content of a template file. You will recall that this refers to everything between the first occurrence of "{contents}" and the last occurrence of "{/contents}" in the template file.
%html%We have now added a {report} layer to the template, and we have removed the "Lawn Happenings" from the template so that the application can fill them in. Layers are placed one after the next in the template content. The {report} layer extends from the first occurrence of "{report}" to the last occurrence of "{/report}" in the template content. Layers have slots just like the template header and footer.
Splitting HTML into separate layers lets us reuse the layers throughout the generated HTML, consolidating duplicated layout information. This template format can also be loaded in a web browser to test changes without running the application. Most templates can also be validated for compliance with HTML and CSS specs.
%code[0]%We are now storing the return value of our FileWriter object's open function. The return value is a Layer object that represents the whole file. It knows the value of the header and footer which were filled in the FileWriter object's open function, but it can still have text filled into its content between the header and footer.
Layer objects have a write_layer function that is passed a layer name followed by keyword parameters that correspond to slots in that layer. Here we are adding two instances of our {report} layer to the content of the template's main Layer object. Recall that values passed in to fill slots are interpreted as HTML Markup by the expand_html_markup function. The "happenings" lists are escaped, then concatenated by this function.
Of course, we can separate the "Lawn Happenings" data from the code by creating a structure for it:
%code[1]%This structure is a list of (day, happenings) tuples that is iterated through in the script. The day and happenings values are treated as HTML Markup
%result[0]%Unfortunately, the HTML code that this generates is different than before we added the {report} layer — the "Happenings" are no longer in separate paragraphs. One way to fix this is to use HTML Markup and avoid mixing actual HTML into the code:
%code[2]%The expand_html_markup function will take the ('p', text) tuples, escape the text, wrap them in HTML paragraph tags then concatenate them.
%result[1]% {/body[lawn4]}HTML Markup is not intended for anything beyond very simple formatting. A more flexible solution is to create another layer to nest within the first.
%html%Now the decision to format individual "Happenings" as paragraphs has been moved to the template in a {happening} layer.
%code[0]%Within the new inner loop we are calling our HTMLTemplate object's format function. This function is similar to the write_layer function except that it returns a RawHTML object instead of adding it to the content of a Layer object. The expand_html_markup function will leave the contents of the RawHTML object intact, so we can use this object within HTML Markup.
{/body[lawn5]}Since it is common to nest layers in Templayer another method is provided that is similar to FileWriter's open function. First we need to rename a slot in the {report} to %contents%:
%html%Now that we have a %contents% slot we can "open" this layer and write into it:
%code[0]%The Layer object has an open_layer function that returns a new layer object. In this case the new Layer object represents a {report} layer that is being written. The Layer object stores everything above the %contents% slot as its header and everything below as its footer. It can have text filled into its content in the same way as our main Layer object. Here we are now using write_layer to fill {happening} layers into {report} layers.
We also renamed the slot in {happening} to %contents%, so we can use the open_layer function on that as well:
%code[1]%We want to send text into this new Layer object, so instead of calling write_layer we call the write function. It takes a single parameter that is interpreted as HTML Markup.
When using Templayer to produce very large files, or when parts of an HTML page take longer to complete, you might want to flush your output part of the way through:
%code[2]%Layer objects have a close_child function that forces any open "child" layers to finalize their output. In this code we are closing the {report} layer. FileWriter objects have a flush function that will send all the output possible. We are first closing our {report} layer so that when we flush the output the whole report will appear.
{/body[lawn6]}Templayer has a number of functions that help integrate with the Django Web Framework. This is an example of a simple Django view. All the examples below assume you are starting from a working project. See the Django documentation about setting up a project.
First place this Templayer template in your project's template directory:
%html%Then we can use get_django_template function to find the template in the project's template directory without having to hard-code the system path in our view:
%code[0]%For this type view function you may also use the django_view decorator. It takes care of creating and cleaning up the file writer object so that you don't need to "import HttpResponse" or remember to "return file_writer.close()" in every view function:
%code[1]% {/body[simple.templayer]}Templayer includes a django_form helper function for working with forms. This function converts a form object to a dictionary of fields suitable for passing to the format, open_layer or write_layer functions:
Start with models like the following:
%code[0]%This template has two slots for each visible field in the form, one for the HTML input field and one for errors related to that field.
%html%This code will populate the form widgets, accept input and redisplay the data (with errors if applicable) when the user clicks "Submit":
%code[1]%Sign the guestbook
Your Name:
Rate the site:
Your Comments:
Existing or third-party Django applications will expect to use the standard Django templating library. To use these applications with Templayer we need to extend the Django template loader and processing mechanism to emulate Django's templates.
This example will show how to use Templayer to create a 404 page and use the "flatpages" application.
First add the django_template_loader to the TEMPLATE_LOADERS in your project's settings.py file:
... TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.load_template_source', 'django.template.loaders.app_directories.load_template_source', ... 'templayer.django_template_loader', ) ...
Also make sure that the "flatpages" application is installed. See the Django flatpages app documentation for details.
Next copy this template into the project's template directory:
%html%Templayer's Django template emulation will look for a module named "templayer_pages.py" in the template directories when trying to load templates. The django_template decorator defines the Django template name that a function will handle. The function may take parameters with names matching values you know will be present in the context dictionary and the decorator will fill in those values for you.
Put this module in the template directory:
%code[0]%Now the 404 handler and the flatpages application will be formatted by Templayer.
{/body[emulate.templayer]}') for obj, name in [ (None,"Template classes"), (templayer.Template,"Template"), (templayer.HTMLTemplate,"HTMLTemplate"), (None,"Output classes"), (templayer.FileWriter,"FileWriter"), (templayer.Layer,"Layer"), (None,"Django integration"), (templayer.django_form,"django_form"), (templayer.django_template_loader,"django_template_loader"), (templayer.django_template,"django_template"), (templayer.django_view,"django_view"), (templayer.get_django_template,"get_django_template"), (None,"Utility functions"), (templayer.html_url_encode,"html_url_encode"), (templayer.html_href,"html_href"), (templayer.html_target,"html_target"), (templayer.html_escape,"html_escape"), (None,None), (None,"HTML Markup (yes, markup language markup)"), (templayer.join,"join"), (templayer.pluralize,"pluralize"), (templayer.urljoin,"urljoin"), (templayer.href,"href"), (templayer.target,"target"), (templayer.BR,"BR"), (templayer.P,"P"), (templayer.I,"I"), (templayer.B,"B"), (templayer.U,"U"), (templayer.entity,"entity"), (templayer.expand_html_markup,"expand_html_markup"), (templayer.RawHTML,"RawHTML"), ]: if name is None: contents.append(' | ')
elif obj is None:
contents.append(' %s ' % name)
doc.append('%s' % name ) else: lname = name if type(obj) != types.ClassType: #dirty hack doc.append('function %s [back to top]' % (name,name) ) lname = lname.replace(" ","_") contents.append('' +
'%s ' %
(lname,name) )
doc.append( html.document( obj, name ) )
contents.append(" |
Template classes Output classes Django integration Utility functions | HTML Markup (yes, markup language markup) |
1. CGI Script: Gordon's Lawn Happenings
|
2. Django Applications and Templayer
|
Our examples will be based on a single-page web site:
<html> <head><title>Gordon's Lawn Happenings</title></head> <body> <h1>Gordon's Lawn Happenings</h1> <h3>Sunday</h3> <p>We've got a groundhog. I will have to stay alert.</p> <p>I lost half a tomato plant to that furry guy.</p> <h3>Monday</h3> <p>The grass grew - I saw it.</p> </body> </html>
We've got a groundhog. I will have to stay alert.
I lost half a tomato plant to that furry guy.
The grass grew - I saw it.
All the example source code may be extracted from the docgen_tutorial.py script that is included with Templayer by running:
python docgen_tutorial.py -s
The first thing we need to do to use this page as a Templayer template file is to add "{contents}" and "{/contents}" labels to the file:
<html> <head><title>Gordon's Lawn Happenings</title></head> <body> <h1>Gordon's Lawn Happenings</h1> {contents} {/contents} <h3>Sunday</h3> <p>We've got a groundhog. I will have to stay alert.</p> <p>I lost half a tomato plant to that furry guy.</p> <h3>Monday</h3> <p>The grass grew - I saw it.</p> </body> </html>
Templayer searches the template file for the first occurrence of "{contents}" and the last occurrence of "{/contents}". This splits the file into header, contents and footer.
The header and footer will appear in Templayer's output. The contents is described in sections below.
import templayer import sys sys.stdout.write("Content-type: text/html\r\n\r\n") tmpl = templayer.HTMLTemplate("lawn2.html") file_writer = tmpl.start_file() file_writer.open() file_writer.close()
This is a minimal CGI script that uses Templayer to display the web site. The result of running this script will be the same as if we just used the static page above.
We first create an HTMLTemplate object to handle parsing of the template above.
The start_file function creates a FileWriter object that will use the our HTMLTemplate object. The FileWriter object handles a single output run for our template. The start_file function allows you to pass in a file-like object to use instead of the standard output. For a CGI script we use the default.
The FileWriter object's open function will be explained below.
No output is written until the close function is called at the end of the script. If you don't get any output from your script it might be because you forgot to call the close function on your FileWriter object.
The next step is to add slots to the header and footer of the template. Slots are like variables that can be filled in by the application. In the template file slot names are wrapped in "%" characters:
<html> <head><title>%title%</title></head> <body> <h1>%title%</h1> {contents} {/contents} <h3>Sunday</h3> <p>We've got a groundhog. I will have to stay alert.</p> <p>I lost half a tomato plant to that furry guy.</p> <h3>Monday</h3> <p>The grass grew - I saw it.</p> <hr> <p>Generated on %date%.</p> </body> </html>
We have added two %title% slots to the header and one %date% slot to the footer of this template.
Please note: "%" is not a special character in Templayer templates. You do not need to escape "%" characters that aren't part of a slot. When filling slots Templayer has a list of slot names and it only replaces those slots, leaving all the other "%" characters in the template intact.
import templayer import time import sys sys.stdout.write("Content-type: text/html\r\n\r\n") tmpl = templayer.HTMLTemplate("lawn3.html") file_writer = tmpl.start_file() file_writer.open(title="Gordon's Lawn Happenings", date=time.asctime()) file_writer.close()
Essentially only one line has changed in the code. The open function takes keyword parameters where the names of the parameters correspond to slots in the header and footer. Here both %title% slots will be filled with "Gordon's Lawn Happenings", and the %date% slot will be filled with the current date and time.
The values passed in to fill slots are interpreted by our HTMLTemplate object. The HTMLTemplate object expects HTML Markup described by the expand_html_markup function. Plain strings are treated as unsafe in HTML Markup and any characters that might be interpreted as HTML are escaped by this function. This means that Templayer's default behaviour is to escape values being filled into HTML templates.
We've got a groundhog. I will have to stay alert.
I lost half a tomato plant to that furry guy.
The grass grew - I saw it.
Generated on Sun Mar 8 17:24:33 2009.
Now we look at what we can do with the content of a template file. You will recall that this refers to everything between the first occurrence of "{contents}" and the last occurrence of "{/contents}" in the template file.
<html> <head><title>%title%</title></head> <body> <h1>%title%</h1> {contents} {report} <h3>%day%</h3> %happenings% {/report} {/contents} <hr> <p>Generated on %date%.</p> </body> </html>
We have now added a {report} layer to the template, and we have removed the "Lawn Happenings" from the template so that the application can fill them in. Layers are placed one after the next in the template content. The {report} layer extends from the first occurrence of "{report}" to the last occurrence of "{/report}" in the template content. Layers have slots just like the template header and footer.
Splitting HTML into separate layers lets us reuse the layers throughout the generated HTML, consolidating duplicated layout information. This template format can also be loaded in a web browser to test changes without running the application. Most templates can also be validated for compliance with HTML and CSS specs.
import templayer import time import sys sys.stdout.write("Content-type: text/html\r\n\r\n") tmpl = templayer.HTMLTemplate("lawn4.html") file_writer = tmpl.start_file() main_layer = file_writer.open(title="Gordon's Lawn Happenings", date=time.asctime()) main_layer.write_layer('report', day="Sunday", happenings=[ "We've got a groundhog. I will have to stay alert.", "I lost half a tomato plant to that furry guy."]) main_layer.write_layer('report', day="Monday", happenings=[ "The grass grew - I saw it."]) file_writer.close()
We are now storing the return value of our FileWriter object's open function. The return value is a Layer object that represents the whole file. It knows the value of the header and footer which were filled in the FileWriter object's open function, but it can still have text filled into its content between the header and footer.
Layer objects have a write_layer function that is passed a layer name followed by keyword parameters that correspond to slots in that layer. Here we are adding two instances of our {report} layer to the content of the template's main Layer object. Recall that values passed in to fill slots are interpreted as HTML Markup by the expand_html_markup function. The "happenings" lists are escaped, then concatenated by this function.
Of course, we can separate the "Lawn Happenings" data from the code by creating a structure for it:
import templayer import time import sys sys.stdout.write("Content-type: text/html\r\n\r\n") reports = [ ('Sunday', ["We've got a groundhog. I will have to stay alert.", "I lost half a tomato plant to that furry guy."]), ('Monday', ["The grass grew - I saw it."]), ] tmpl = templayer.HTMLTemplate("lawn4.html") file_writer = tmpl.start_file() main_layer = file_writer.open(title="Gordon's Lawn Happenings", date=time.asctime()) for d, h in reports: main_layer.write_layer('report', day=d, happenings=h) file_writer.close()
This structure is a list of (day, happenings) tuples that is iterated through in the script. The day and happenings values are treated as HTML Markup
Generated on Sun Mar 8 17:24:33 2009.
Unfortunately, the HTML code that this generates is different than before we added the {report} layer — the "Happenings" are no longer in separate paragraphs. One way to fix this is to use HTML Markup and avoid mixing actual HTML into the code:
import templayer import time import sys sys.stdout.write("Content-type: text/html\r\n\r\n") reports = [ ('Sunday', [('p',"We've got a groundhog. I will have to stay alert."), ('p',"I lost half a tomato plant to that furry guy.")]), ('Monday', [('p',"The grass grew - I saw it.")]), ] tmpl = templayer.HTMLTemplate("lawn4.html") file_writer = tmpl.start_file() main_layer = file_writer.open(title="Gordon's Lawn Happenings", date=time.asctime()) for d, h in reports: main_layer.write_layer('report', day=d, happenings=h) file_writer.close()
The expand_html_markup function will take the ('p', text) tuples, escape the text, wrap them in HTML paragraph tags then concatenate them.
We've got a groundhog. I will have to stay alert.
I lost half a tomato plant to that furry guy.
The grass grew - I saw it.
Generated on Sun Mar 8 17:24:33 2009.
HTML Markup is not intended for anything beyond very simple formatting. A more flexible solution is to create another layer to nest within the first.
<html> <head><title>%title%</title></head> <body> <h1>%title%</h1> {contents} {report} <h3>%day%</h3> %happenings% {/report} {happening}<p>%what%</p> {/happening} {/contents} <hr> <p>Generated on %date%.</p> </body> </html>
Now the decision to format individual "Happenings" as paragraphs has been moved to the template in a {happening} layer.
import templayer import time import sys sys.stdout.write("Content-type: text/html\r\n\r\n") reports = [ ('Sunday', ["We've got a groundhog. I will have to stay alert.", "I lost half a tomato plant to that furry guy."]), ('Monday', ["The grass grew - I saw it."]), ] tmpl = templayer.HTMLTemplate("lawn5.html") file_writer = tmpl.start_file() main_layer = file_writer.open(title="Gordon's Lawn Happenings", date=time.asctime()) for d, h in reports: happening_list = [] for w in h: formatted = tmpl.format('happening', what=w) happening_list.append(formatted) main_layer.write_layer('report', day=d, happenings=happening_list) file_writer.close()
Within the new inner loop we are calling our HTMLTemplate object's format function. This function is similar to the write_layer function except that it returns a RawHTML object instead of adding it to the content of a Layer object. The expand_html_markup function will leave the contents of the RawHTML object intact, so we can use this object within HTML Markup.
Since it is common to nest layers in Templayer another method is provided that is similar to FileWriter's open function. First we need to rename a slot in the {report} to %contents%:
<html> <head><title>%title%</title></head> <body> <h1>%title%</h1> {contents} {report} <h3>%day%</h3> %contents% {/report} {happening}<p>%contents%</p> {/happening} {/contents} <hr> <p>Generated on %date%.</p> </body> </html>
Now that we have a %contents% slot we can "open" this layer and write into it:
import templayer import time import sys sys.stdout.write("Content-type: text/html\r\n\r\n") reports = [ ('Sunday', ["We've got a groundhog. I will have to stay alert.", "I lost half a tomato plant to that furry guy."]), ('Monday', ["The grass grew - I saw it."]), ] tmpl = templayer.HTMLTemplate("lawn6.html") file_writer = tmpl.start_file() main_layer = file_writer.open(title="Gordon's Lawn Happenings", date=time.asctime()) for d, h in reports: report_layer = main_layer.open_layer('report', day=d) for happening in h: report_layer.write_layer('happening', contents=happening) file_writer.close()
The Layer object has an open_layer function that returns a new layer object. In this case the new Layer object represents a {report} layer that is being written. The Layer object stores everything above the %contents% slot as its header and everything below as its footer. It can have text filled into its content in the same way as our main Layer object. Here we are now using write_layer to fill {happening} layers into {report} layers.
We also renamed the slot in {happening} to %contents%, so we can use the open_layer function on that as well:
import templayer import time import sys sys.stdout.write("Content-type: text/html\r\n\r\n") reports = [ ('Sunday', ["We've got a groundhog. I will have to stay alert.", "I lost half a tomato plant to that furry guy."]), ('Monday', ["The grass grew - I saw it."]), ] tmpl = templayer.HTMLTemplate("lawn6.html") file_writer = tmpl.start_file() main_layer = file_writer.open(title="Gordon's Lawn Happenings", date=time.asctime()) for d, h in reports: report_layer = main_layer.open_layer('report', day=d) for happening in h: happening_layer = report_layer.open_layer('happening') happening_layer.write(happening) file_writer.close()
We want to send text into this new Layer object, so instead of calling write_layer we call the write function. It takes a single parameter that is interpreted as HTML Markup.
When using Templayer to produce very large files, or when parts of an HTML page take longer to complete, you might want to flush your output part of the way through:
import templayer import time import sys sys.stdout.write("Content-type: text/html\r\n\r\n") reports = [ ('Sunday', ["We've got a groundhog. I will have to stay alert.", "I lost half a tomato plant to that furry guy."]), ('Monday', ["The grass grew - I saw it."]), ] tmpl = templayer.HTMLTemplate("lawn6.html") file_writer = tmpl.start_file() main_layer = file_writer.open(title="Gordon's Lawn Happenings", date=time.asctime()) for d, h in reports: report_layer = main_layer.open_layer('report', day=d) for happening in h: happening_layer = report_layer.open_layer('happening') happening_layer.write(happening) main_layer.close_child() file_writer.flush() file_writer.close()
Layer objects have a close_child function that forces any open "child" layers to finalize their output. In this code we are closing the {report} layer. FileWriter objects have a flush function that will send all the output possible. We are first closing our {report} layer so that when we flush the output the whole report will appear.
Templayer has a number of functions that help integrate with the Django Web Framework. This is an example of a simple Django view. All the examples below assume you are starting from a working project. See the Django documentation about setting up a project.
First place this Templayer template in your project's template directory:
<html> <head><title>%title%</title></head> <body> <h1>%title%</h1> {contents} {/contents} </body> </html>
Then we can use get_django_template function to find the template in the project's template directory without having to hard-code the system path in our view:
from django.http import HttpResponse import datetime import templayer tmpl = templayer.get_django_template("simple.templayer.html") def current_datetime(request): file_writer = tmpl.start_file(HttpResponse()) now = datetime.datetime.now() contents = file_writer.open(title="Current Date and Time") contents.write("It is now %s." % now) return file_writer.close()
For this type view function you may also use the django_view decorator. It takes care of creating and cleaning up the file writer object so that you don't need to "import HttpResponse" or remember to "return file_writer.close()" in every view function:
import datetime import templayer tmpl = templayer.get_django_template("simple.templayer.html") @templayer.django_view(tmpl) def current_datetime(file_writer, request): now = datetime.datetime.now() contents = file_writer.open(title="Current Date and Time") contents.write("It is now %s." % now)
Templayer includes a django_form helper function for working with forms. This function converts a form object to a dictionary of fields suitable for passing to the format, open_layer or write_layer functions:
Start with models like the following:
from django.db import models RATING_CHOICES = ( ('G', "Great!"), ('A', "Average"), ('U', "Uninteresting"), ) class Entry(models.Model): name = models.CharField("Your Name", max_length=100) rating = models.CharField("Rate this site", max_length=1, choices=RATING_CHOICES) comment = models.TextField("Your Comments (optional)", blank=True) posted = models.DateTimeField(auto_now_add=True)
This template has two slots for each visible field in the form, one for the HTML input field and one for errors related to that field.
<html> <head><title>%title%</title></head> <body> <h1>%title%</h1> {contents} {new_entry} <form method="POST"> <h2>Sign the guestbook</h2> Your Name: %form.name% %form.name.errors%<br/> Rate the site: %form.rating% %form.rating.errors%<br/> Your Comments:<br/> %form.comment% %form.comment.errors%<br/> <input type="submit" value="Submit"/> </form> {/new_entry} {/contents} </body> </html>
This code will populate the form widgets, accept input and redisplay the data (with errors if applicable) when the user clicks "Submit":
from book.models import Entry from django.forms import ModelForm import templayer tmpl = templayer.get_django_template("book.templayer.html") class EntryForm(ModelForm): class Meta: model = Entry @templayer.django_view(tmpl) def guest_book(file_layer, request): if request.POST: entry_form = EntryForm(request.POST) # TODO: do something with the data if all is well else: entry_form = EntryForm() contents = file_writer.open(title="Guest Book") contents.write_layer("new_entry", **templayer.django_form(entry_form))
Sign the guestbook
Your Name:
Rate the site:
Your Comments:
Existing or third-party Django applications will expect to use the standard Django templating library. To use these applications with Templayer we need to extend the Django template loader and processing mechanism to emulate Django's templates.
This example will show how to use Templayer to create a 404 page and use the "flatpages" application.
First add the django_template_loader to the TEMPLATE_LOADERS in your project's settings.py file:
... TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.load_template_source', 'django.template.loaders.app_directories.load_template_source', ... 'templayer.django_template_loader', ) ...
Also make sure that the "flatpages" application is installed. See the Django flatpages app documentation for details.
Next copy this template into the project's template directory:
<html> <head><title>%title%</title></head> <body> <h1>%title%</h1> {contents} {show_404} The url %url% could not be found. {/show_404} {flatpage_body} %contents% {/flatpage_body} {/contents} </body> </html>
Templayer's Django template emulation will look for a module named "templayer_pages.py" in the template directories when trying to load templates. The django_template decorator defines the Django template name that a function will handle. The function may take parameters with names matching values you know will be present in the context dictionary and the decorator will fill in those values for you.
Put this module in the template directory:
import templayer tmpl = templayer.get_django_template("emulate.templayer.html") @templayer.django_template(tmpl, "404.html") def show_404(file_writer, context, request_path): contents = file_writer.open(title="Page not found") # request_path == context['request_path'] contents.write_layer("show_404", url=request_path) @templayer.django_template(tmpl, "flatpages/default.html") def show_flatpage(file_writer, context, flatpage): contents = file_writer.open(title=flatpage.title) fp_contents = contents.open_layer("flatpage_body") # the flatpage content is HTML, so don't escape it. fp_contents.write(templayer.RawHTML(flatpage.content))
Now the 404 handler and the flatpages application will be formatted by Templayer.