template
Module¶
This file is part of the web2py Web Framework
License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
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.
-
-
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
-
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.
-
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>¶
-
gluon.template.
get_parsed
(text)[source]¶ Returns the indented python code of text. Useful for unit testing.
-
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'