12. Template Rendering¶
-
flask.
render_template
(template_name_or_list, **context)[source]¶ Renders a template from the template folder with the given context.
Parameters: - template_name_or_list – the name of the template to be rendered, or an iterable with template names the first one existing will be rendered
- context – the variables that should be available in the context of the template.
-
flask.
render_template_string
(source, **context)[source]¶ Renders a template from the given template source string with the given context. Template variables will be autoescaped.
Parameters: - source – the source code of the template to be rendered
- context – the variables that should be available in the context of the template.
-
flask.
get_template_attribute
(template_name, attribute)[source]¶ Loads a macro (or variable) a template exports. This can be used to invoke a macro from within Python code. If you for example have a template named
_cider.html
with the following contents:{% macro hello(name) %}Hello {{ name }}!{% endmacro %}
You can access this from Python code like this:
hello = get_template_attribute('_cider.html', 'hello') return hello('World')
New in version 0.2.
Parameters: - template_name – the name of the template
- attribute – the name of the variable of macro to access