Skip to content

InclusionTag

Base classes

Name Children Inherits
StandaloneTag
jinjarope.tags
Tag that renders to a single output without content block.

⋔ Inheritance diagram

graph TD
  97041793791008["tags.InclusionTag"]
  97041793760816["tags.StandaloneTag"]
  97041793788816["tags.BaseTemplateTag"]
  97041787232064["ext.Extension"]
  128674497047040["builtins.object"]
  97041793760816 --> 97041793791008
  97041793788816 --> 97041793760816
  97041787232064 --> 97041793788816
  128674497047040 --> 97041787232064

🛈 DocStrings

Bases: StandaloneTag

Tag that includes other templates with context.

Source code in src/jinjarope/tags.py
class InclusionTag(StandaloneTag):
    """Tag that includes other templates with context."""

    template_name: str | None = None
    safe_output: ClassVar[bool] = True

    def render(self, *args: Any, **kwargs: Any) -> str:
        """Render included template with context."""
        template_names = self.get_template_names(*args, **kwargs)
        template = (
            self.environment.get_template(template_names)
            if isinstance(template_names, str)
            else self.environment.select_template(template_names)
        )

        if not self.context:
            msg = "Context not available"
            raise RuntimeError(msg)

        context = template.new_context(
            {**self.context.get_all(), **self.get_context(*args, **kwargs)},
            shared=True,
        )
        return template.render(context)

    def get_context(self, *args: Any, **kwargs: Any) -> dict[str, Any]:
        """Get additional context for template."""
        return {}

    def get_template_names(self, *args: Any, **kwargs: Any) -> str | Sequence[str]:
        """Get template name(s) to include."""
        if not self.template_name:
            msg = "InclusionTag requires 'template_name' or 'get_template_names()'"
            raise RuntimeError(msg)
        return self.template_name

get_context

get_context(*args: Any, **kwargs: Any) -> dict[str, Any]

Get additional context for template.

Source code in src/jinjarope/tags.py
def get_context(self, *args: Any, **kwargs: Any) -> dict[str, Any]:
    """Get additional context for template."""
    return {}

get_template_names

get_template_names(*args: Any, **kwargs: Any) -> str | Sequence[str]

Get template name(s) to include.

Source code in src/jinjarope/tags.py
def get_template_names(self, *args: Any, **kwargs: Any) -> str | Sequence[str]:
    """Get template name(s) to include."""
    if not self.template_name:
        msg = "InclusionTag requires 'template_name' or 'get_template_names()'"
        raise RuntimeError(msg)
    return self.template_name

render

render(*args: Any, **kwargs: Any) -> str

Render included template with context.

Source code in src/jinjarope/tags.py
def render(self, *args: Any, **kwargs: Any) -> str:
    """Render included template with context."""
    template_names = self.get_template_names(*args, **kwargs)
    template = (
        self.environment.get_template(template_names)
        if isinstance(template_names, str)
        else self.environment.select_template(template_names)
    )

    if not self.context:
        msg = "Context not available"
        raise RuntimeError(msg)

    context = template.new_context(
        {**self.context.get_all(), **self.get_context(*args, **kwargs)},
        shared=True,
    )
    return template.render(context)

:fa-brands-github: Show source on GitHub