Renderers#

The “Renderer” is a class that is created in order to generate the content of a template.

Fabricius ships many by default, you can use them, or create your own if you feel the need to.

from fabricius.renderers import Renderer

class MyRenderer(Renderer):

    # You must implement the "render" method, this will be called by Fabricius.
    def render(self, content: str):
        # Inside of the Renderer class, the "data" property is available.
        # This is where the data is stored.

        final_content = render_content(content=content, data=self.data)

        return final_content

renderer = MyRenderer({"name": "John"})
final_content = renderer.render("Hello {{ name }}")
print(final_content)
# Hello John

The following is the list of the available renderer packaged with Fabricius. It contains Python’s str.format, string template, Mustache & Jinja.

Hint

If you’re using the File object, you can use methods File.use_jinja() to set the renderer to one of Fabricius’s available. To use your own Renderer, use File.with_renderer().

class fabricius.renderers.JinjaRenderer(data: dict[str, Any])#
name: ClassVar[str | None] = 'Jinja Template'#

The name of the renderer, not necessary, but suggested to add.

render(content: str) str#

This method will process a given string, the template input and return the processed template as a string too.

Parameters:

content (str) – The template

Returns:

The result of the processed template.

Return type:

str

class fabricius.renderers.ChevronRenderer(data: dict[str, Any])#
name: ClassVar[str | None] = 'Chevron (Moustache)'#

The name of the renderer, not necessary, but suggested to add.

render(content: str) str#

This method will process a given string, the template input and return the processed template as a string too.

Parameters:

content (str) – The template

Returns:

The result of the processed template.

Return type:

str

class fabricius.renderers.PythonFormatRenderer(data: dict[str, Any])#
name: ClassVar[str | None] = 'Python str.format'#

The name of the renderer, not necessary, but suggested to add.

render(content: str) str#

This method will process a given string, the template input and return the processed template as a string too.

Parameters:

content (str) – The template

Returns:

The result of the processed template.

Return type:

str

class fabricius.renderers.StringTemplateRenderer(data: dict[str, Any], *, safe: bool = True)#
name: ClassVar[str | None] = 'Python string.Template'#

The name of the renderer, not necessary, but suggested to add.

safe: bool#

Indicate if the renderer should use string.Template.safe_substitute() or string.Template.substitute()

render(content: str) str#

This method will process a given string, the template input and return the processed template as a string too.

Parameters:

content (str) – The template

Returns:

The result of the processed template.

Return type:

str