Warning: Bocadillo is now UNMAINTAINED. Users are recommended to migrate to a supported alternative, such as Starlette or FastAPI. Please see #344 for more information.

bocadillo.templates

Templates

Templates(self, *args, directory:Union[str, pathlib.Path]='templates', context:dict=None)

This class provides templating capabilities.

This is a light wrapper around jinja2.

See also Templates for detail.

Parameters are also stored as attributes and can be accessed or modified at runtime.

Parameters

  • directory (str): the directory where templates should be searched for. Defaults to "templates" relative to the current working directory.
  • context (dict, optional): global template variables.

render

Templates.render(self, filename:str, *args:dict, **kwargs:Any) -> str

Render a template asynchronously.

Can only be used within async functions.

Parameters

  • name (str): name of the template, located inside templates_dir. The trailing underscore avoids collisions with a potential context variable named name.
  • *args (dict): context variables to inject in the template.
  • *kwargs (str): context variables to inject in the template.

render_sync

Templates.render_sync(self, filename:str, *args:dict, **kwargs:Any) -> str

Render a template synchronously.

See Also

Templates.render for the accepted arguments.

render_string

Templates.render_string(self, source:str, *args:dict, **kwargs:Any) -> str

Render a template from a string (synchronously).

Parameters

  • source (str): a template given as a string.

See Also

Templates.render for the other accepted arguments.