template Module

This file is part of the web2py Web Framework
Author: Thadeus Burgess
- 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.


{{ block test }}
    This is default block test
{{ end }}

Adds an element to the nodes.

Parameters:node – Node object or string to append.

Extends the list of nodes with another BlockNode class.

Parameters:other – BlockNode or Content object to extend from.

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

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


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.

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

  • 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 = ('{{', '}}')

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.

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>

Reindents a string of unindented python code.


Returns the parsed template with correct indentation.

Used to make it easier to port to python3.


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]
  • 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

  • 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
>>> render()
'hello world'
>>> render(content='abc')
>>> render(content="abc'")
>>> render(content=''''a"'bc''')
>>> render(content='a\nbc')
>>> render(content='a"bcd"e')
>>> render(content="'''a\nc'''")
>>> render(content="'''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'''}}")
>>> render(content='{{for i in range(3):\n=i\npass}}')