Working with templatesΒΆ

In fastlife, templates are declarable in multiple way, in the boostrap with poetry, we learn the simplest way to use a template, using the template attributes of the @view_config decorator

from fastlife import view_config

@view_config("hello_world", "/",  template="HelloWorld.jinja")
def hello_world() -> dict[str, str]:
    return {}

But, there is an other option to achieve the same result that is a bit more verbose but more flexible.

from fastlife import Response
from fastlife.templates import Template, template

@view_config("hello_world", "/")
def hello_world(
    template: Annotated[Template, template("HelloWorld.jinja")],
) -> Response:
    return template()

In this example, we use a FastAPI dependency that will render the HelloWorld.jinja template file.

Tip

This method is very usefull in two situation:

  • We have to choose between multiple templates in the view depending on other parameter.

  • We want to call the view from another vue using composition.

From a testing point of view rendering template inside the view, we are not able to tests the rendering parameters of the view anymore. And we are forced to tests with the WebTestClient.