API reference¶
An implementation of Curly language.
Curly contains only 4 modules (including rudiment stuff for CLI): lexing, parsing, template and utils.
The control flow is: to render the template, you need to create
an instance of curly.template.Template
. Template
has a parsed AST tree (it is created on its initialization
phase) of the real template and a number of routines (main is
curly.template.Template.render()
) to calculate the value of the
given template string.
How does curly.template.Template
got that AST tree?
Well, that’s why you need lexing and parsing. Lexing splits raw text
string into a list of tokens: a typed classes which encapsulate a
chunks of text and describe them in terms of templating language (see
curly.lexer.tokenize()
). Parsing takes the list of tokens and
makes tree structure with determined way of calculating template (only
environment and text are required.). Check curly.parser.parse()
for details.
-
curly.
render
(text, context)[source]¶ Renders given text based on the context variables.
This function uses
curly.env.DEFAULT_ENV
as a default environment, build a template from thetext
parameter and renders it according to thecontext
.This is not the most effective method of calculating template because you need to parse it on any call. If you want the most efficient way, precompile template first.
>>> from curly.template import Template >>> template = Template(text) >>> result1 = template.render(context1) >>> result2 = template.render(context2)
Parameters: Returns: Rendered template.
Return type: Raises: curly.exceptions.CurlyEvaluateError
: if it is not possible to evaluate expression within a context.curly.exceptions.CurlyLexerError
: if it is not possible to perform lexing analysis of the template.curly.exceptions.CurlyParserError
: if it is not possible to parse template.