template Module

This file is part of the web2py Web Framework
Author: Thadeus Burgess
Contributors:
- Massimo Di Pierro for creating the original gluon/template.py
- Jonathan Lundell for extensively testing the regex on Jython.
- Limodou (creater of uliweb) who inspired the block-element support for web2py.

Templating syntax

class gluon.template.BlockNode(name='', pre_extend=False, delimiters=('{{', '}}'))[source]

Bases: gluon.template.Node

Block Container.

This Node can contain other Nodes and will render in a hierarchical order of when nodes were added.

ie:

{{ block test }}
    This is default block test
{{ end }}
append(node)[source]

Adds an element to the nodes.

Parameters:node – Node object or string to append.
extend(other)[source]

Extends the list of nodes with another BlockNode class.

Parameters:other – BlockNode or Content object to extend from.
output(blocks)[source]

Merges all nodes into a single string.

Parameters:blocks – Dictionary of blocks that are extending from this template.
class gluon.template.Content(name='ContentBlock', pre_extend=False)[source]

Bases: gluon.template.BlockNode

Parent Container – Used as the root level BlockNode.

Contains functions that operate as such.

Parameters:name – Unique name for this BlockNode
append(node)[source]

Adds a node to list. If it is a BlockNode then we assign a block for it.

clear_content()[source]
extend(other)[source]

Extends the objects list of nodes with another objects nodes

insert(other, index=0)[source]

Inserts object at index.

You may pass a list of objects and have them inserted.

class gluon.template.DummyResponse[source]
write(data, escape=True)[source]
class gluon.template.NOESCAPE(text)[source]

A little helper to avoid escaping.

xml()[source]
class gluon.template.Node(value=None, pre_extend=False)[source]

Bases: object

Basic Container Object

class gluon.template.SuperNode(name='', pre_extend=False)[source]

Bases: gluon.template.Node

class gluon.template.TemplateParser(text, name='ParserContainer', context={}, path='views/', writer='response.write', lexers={}, delimiters=('{{', '}}'), _super_nodes=[])[source]

Bases: object

Parse all blocks

Parameters:
  • text – text to parse
  • context – context to parse in
  • path – folder path to templates
  • writer – string of writer class to use
  • lexers – dict of custom lexers to use.
  • delimiters – for example (‘{{‘,’}}’)
  • _super_nodes – a list of nodes to check for inclusion this should only be set by “self.extend” It contains a list of SuperNodes from a child template that need to be handled.
default_delimiters = ('{{', '}}')
extend(filename)[source]

Extends filename. Anything not declared in a block defined by the parent will be placed in the parent templates {{include}} block.

include(content, filename)[source]

Includes filename here.

parse(text)[source]
r_multiline = <_sre.SRE_Pattern object>
r_tag = <_sre.SRE_Pattern object>
re_block = <_sre.SRE_Pattern object>
re_pass = <_sre.SRE_Pattern object>
re_unblock = <_sre.SRE_Pattern object>
reindent(text)[source]

Reindents a string of unindented python code.

to_string()[source]

Returns the parsed template with correct indentation.

Used to make it easier to port to python3.

gluon.template.get_parsed(text)[source]

Returns the indented python code of text. Useful for unit testing.

gluon.template.output_aux(node, blocks)[source]
gluon.template.parse_template(filename, path='views/', context={}, lexers={}, delimiters=('{{', '}}'))[source]
Parameters:
  • filename – can be a view filename in the views folder or an input stream
  • path – is the path of a views folder
  • context – is a dictionary of symbols used to render the template
  • lexers – dict of custom lexers to use
  • delimiters – opening and closing tags
gluon.template.render(content='hello world', stream=None, filename=None, path=None, context={}, lexers={}, delimiters=('{{', '}}'), writer='response.write')[source]

Generic render function

Parameters:
  • content – default content
  • stream – file-like obj to read template from
  • filename – where to find template
  • path – base path for templates
  • context – env
  • lexers – custom lexers to use
  • delimiters – opening and closing tags
  • writer – where to inject the resulting stream
Example::
>>> render()
'hello world'
>>> render(content='abc')
'abc'
>>> render(content="abc'")
"abc'"
>>> render(content=''''a"'bc''')
'a"'bc'
>>> render(content='a\nbc')
'a\nbc'
>>> render(content='a"bcd"e')
'a"bcd"e'
>>> render(content="'''a\nc'''")
"'''a\nc'''"
>>> render(content="'''a\'c'''")
"'''a'c'''"
>>> render(content='{{for i in range(a):}}{{=i}}<br />{{pass}}', context=dict(a=5))
'0<br />1<br />2<br />3<br />4<br />'
>>> render(content='{%for i in range(a):%}{%=i%}<br />{%pass%}', context=dict(a=5),delimiters=('{%','%}'))
'0<br />1<br />2<br />3<br />4<br />'
>>> render(content="{{='''hello\nworld'''}}")
'hello\nworld'
>>> render(content='{{for i in range(3):\n=i\npass}}')
'012'