OpenQuizz
Une application de gestion des contenus pédagogiques
Template Class Reference
Inheritance diagram for Template:
Collaboration diagram for Template:

Public Member Functions

def __new__ (cls, source, block_start_string=BLOCK_START_STRING, block_end_string=BLOCK_END_STRING, variable_start_string=VARIABLE_START_STRING, variable_end_string=VARIABLE_END_STRING, comment_start_string=COMMENT_START_STRING, comment_end_string=COMMENT_END_STRING, line_statement_prefix=LINE_STATEMENT_PREFIX, line_comment_prefix=LINE_COMMENT_PREFIX, trim_blocks=TRIM_BLOCKS, lstrip_blocks=LSTRIP_BLOCKS, newline_sequence=NEWLINE_SEQUENCE, keep_trailing_newline=KEEP_TRAILING_NEWLINE, extensions=(), optimized=True, undefined=Undefined, finalize=None, autoescape=False, enable_async=False)
 
def from_code (cls, environment, code, globals, uptodate=None)
 
def from_module_dict (cls, environment, module_dict, globals)
 
def render (self, *args, **kwargs)
 
def render_async (self, *args, **kwargs)
 
def stream (self, *args, **kwargs)
 
def generate (self, *args, **kwargs)
 
def generate_async (self, *args, **kwargs)
 
def new_context (self, vars=None, shared=False, locals=None)
 
def make_module (self, vars=None, shared=False, locals=None)
 
def make_module_async (self, vars=None, shared=False, locals=None)
 
def module (self)
 
def get_corresponding_lineno (self, lineno)
 
def is_up_to_date (self)
 
def debug_info (self)
 
def __repr__ (self)
 

Static Public Attributes

 environment_class
 

Detailed Description

The central template object.  This class represents a compiled template
and is used to evaluate it.

Normally the template object is generated from an :class:`Environment` but
it also has a constructor that makes it possible to create a template
instance directly using the constructor.  It takes the same arguments as
the environment constructor but it's not possible to specify a loader.

Every template object has a few methods and members that are guaranteed
to exist.  However it's important that a template object should be
considered immutable.  Modifications on the object are not supported.

Template objects created from the constructor rather than an environment
do have an `environment` attribute that points to a temporary environment
that is probably shared with other templates created with the constructor
and compatible settings.

>>> template = Template('Hello {{ name }}!')
>>> template.render(name='John Doe') == u'Hello John Doe!'
True
>>> stream = template.stream(name='John Doe')
>>> next(stream) == u'Hello John Doe!'
True
>>> next(stream)
Traceback (most recent call last):
    ...
StopIteration

Member Function Documentation

◆ __new__()

def __new__ (   cls,
  source,
  block_start_string = BLOCK_START_STRING,
  block_end_string = BLOCK_END_STRING,
  variable_start_string = VARIABLE_START_STRING,
  variable_end_string = VARIABLE_END_STRING,
  comment_start_string = COMMENT_START_STRING,
  comment_end_string = COMMENT_END_STRING,
  line_statement_prefix = LINE_STATEMENT_PREFIX,
  line_comment_prefix = LINE_COMMENT_PREFIX,
  trim_blocks = TRIM_BLOCKS,
  lstrip_blocks = LSTRIP_BLOCKS,
  newline_sequence = NEWLINE_SEQUENCE,
  keep_trailing_newline = KEEP_TRAILING_NEWLINE,
  extensions = (),
  optimized = True,
  undefined = Undefined,
  finalize = None,
  autoescape = False,
  enable_async = False 
)

◆ __repr__()

def __repr__ (   self)

◆ debug_info()

def debug_info (   self)
The debug info mapping.

◆ from_code()

def from_code (   cls,
  environment,
  code,
  globals,
  uptodate = None 
)
Creates a template object from compiled code and the globals.  This
is used by the loaders and environment to create a template object.

◆ from_module_dict()

def from_module_dict (   cls,
  environment,
  module_dict,
  globals 
)
Creates a template object from a module.  This is used by the
module loader to create a template object.

.. versionadded:: 2.4

◆ generate()

def generate (   self,
args,
**  kwargs 
)
For very large templates it can be useful to not render the whole
template at once but evaluate each statement after another and yield
piece for piece.  This method basically does exactly that and returns
a generator that yields one item after another as unicode strings.

It accepts the same arguments as :meth:`render`.

◆ generate_async()

def generate_async (   self,
args,
**  kwargs 
)
An async version of :meth:`generate`.  Works very similarly but
returns an async iterator instead.

◆ get_corresponding_lineno()

def get_corresponding_lineno (   self,
  lineno 
)
Return the source line number of a line number in the
generated bytecode as they are not in sync.

◆ is_up_to_date()

def is_up_to_date (   self)
If this variable is `False` there is a newer version available.

◆ make_module()

def make_module (   self,
  vars = None,
  shared = False,
  locals = None 
)
This method works like the :attr:`module` attribute when called
without arguments but it will evaluate the template on every call
rather than caching it.  It's also possible to provide
a dict which is then used as context.  The arguments are the same
as for the :meth:`new_context` method.

◆ make_module_async()

def make_module_async (   self,
  vars = None,
  shared = False,
  locals = None 
)
As template module creation can invoke template code for
asynchronous executions this method must be used instead of the
normal :meth:`make_module` one.  Likewise the module attribute
becomes unavailable in async mode.

◆ module()

def module (   self)
The template as module.  This is used for imports in the
template runtime but is also useful if one wants to access
exported template variables from the Python layer:

>>> t = Template('{% macro foo() %}42{% endmacro %}23')
>>> str(t.module)
'23'
>>> t.module.foo() == u'42'
True

This attribute is not available if async mode is enabled.

◆ new_context()

def new_context (   self,
  vars = None,
  shared = False,
  locals = None 
)
Create a new :class:`Context` for this template.  The vars
provided will be passed to the template.  Per default the globals
are added to the context.  If shared is set to `True` the data
is passed as is to the context without adding the globals.

`locals` can be a dict of local variables for internal usage.

◆ render()

def render (   self,
args,
**  kwargs 
)
This method accepts the same arguments as the `dict` constructor:
A dict, a dict subclass or some keyword arguments.  If no arguments
are given the context will be empty.  These two calls do the same::

    template.render(knights='that say nih')
    template.render({'knights': 'that say nih'})

This will return the rendered template as unicode string.

Reimplemented in NativeTemplate.

◆ render_async()

def render_async (   self,
args,
**  kwargs 
)
This works similar to :meth:`render` but returns a coroutine
that when awaited returns the entire rendered template string.  This
requires the async feature to be enabled.

Example usage::

    await template.render_async(knights='that say nih; asynchronously')

◆ stream()

def stream (   self,
args,
**  kwargs 
)
Works exactly like :meth:`generate` but returns a
:class:`TemplateStream`.

Field Documentation

◆ environment_class

environment_class
static

The documentation for this class was generated from the following file: