quart.templating module

class quart.templating.DispatchingJinjaLoader(app: Quart)

Bases: jinja2.loaders.BaseLoader

Quart specific Jinja2 Loader.

This changes the default sourcing to consider the app and blueprints.

get_source(environment: jinja2.environment.Environment, template: str) Tuple[str, Optional[str], Optional[Callable[[], bool]]]

Returns the template source from the environment.

This considers the loaders on the app and blueprints.

list_templates() List[str]

Returns a list of all available templates in environment.

This considers the loaders on the app and blueprints.

class quart.templating.Environment(app: Quart, **options: Any)

Bases: jinja2.environment.Environment

Quart specific Jinja2 Environment.

This changes the default Jinja2 loader to use the DispatchingJinjaLoader, and enables async Jinja by default.

async quart.templating.render_template(template_name_or_list: Union[str, List[str]], **context: Any) str

Render the template with the context given.

Parameters
  • template_name_or_list – Template name to render of a list of possible template names.

  • context – The variables to pass to the template.

async quart.templating.render_template_string(source: str, **context: Any) str

Render the template source with the context given.

Parameters
  • source – The template source code.

  • context – The variables to pass to the template.

async quart.templating.stream_template(template_name_or_list: Union[str, jinja2.environment.Template, List[Union[str, jinja2.environment.Template]]], **context: Any) AsyncIterator[str]

Render a template by name with the given context as a stream.

This returns an iterator of strings, which can be used as a streaming response from a view.

Parameters
  • template_name_or_list – The name of the template to render. If a list is given, the first name to exist will be rendered.

  • context – The variables to make available in the template.

async quart.templating.stream_template_string(source: str, **context: Any) AsyncIterator[str]

Render a template from the given source with the context as a stream.

This returns an iterator of strings, which can be used as a streaming response from a view.

Parameters
  • source – The source code of the template to render.

  • context – The variables to make available in the template.