Skip to content

templates ¤

This module contains our definitions of templates.

Modules:

  • filters

    This module contains Jinja filters.

Classes:

Template ¤

Template(
    env_or_directory: str | Environment,
    base_template: str,
    context: dict[str, Any] | None = None,
    filters: dict[str, Any] | None = None,
)

Shellman templates.

Parameters:

  • env_or_directory (str | Environment) –

    Jinja environment or directory to load environment from.

  • base_template (str) –

    The template file to use.

  • context (dict[str, Any] | None, default: None ) –

    Base context to render with.

  • filters (dict[str, Any] | None, default: None ) –

    Base filters to add to the environment.

Methods:

  • render

    Render the template.

Attributes:

Source code in src/shellman/templates/__init__.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def __init__(
    self,
    env_or_directory: str | Environment,
    base_template: str,
    context: dict[str, Any] | None = None,
    filters: dict[str, Any] | None = None,
):
    """Initialize the template.

    Parameters:
        env_or_directory: Jinja environment or directory to load environment from.
        base_template: The template file to use.
        context: Base context to render with.
        filters: Base filters to add to the environment.
    """
    if isinstance(env_or_directory, Environment):
        self.env = env_or_directory
    elif isinstance(env_or_directory, str):
        self.env = _get_env(env_or_directory)
    else:
        raise TypeError(env_or_directory)

    if filters is None:
        filters = {}

    self.env.filters.update(FILTERS)
    self.env.filters.update(filters)
    self.base_template = base_template
    self.context = context or {}
    self.__template: Template = None  # type: ignore[assignment]

template property ¤

template: Template

The corresponding Jinja template.

render ¤

render(**kwargs: Any) -> str

Render the template.

Parameters:

  • **kwargs (Any, default: {} ) –

    Keyword arguments passed to Jinja's render method.

Returns:

  • str

    The rendered text.

Source code in src/shellman/templates/__init__.py
80
81
82
83
84
85
86
87
88
89
90
91
92
def render(self, **kwargs: Any) -> str:
    """Render the template.

    Parameters:
        **kwargs: Keyword arguments passed to Jinja's render method.


    Returns:
        The rendered text.
    """
    context = deepcopy(self.context)
    context.update(kwargs)
    return self.template.render(**context).rstrip("\n")