tag_formatter - Django-Components" > tag_formatter - Django-Components" >
Skip to content

tag_formatter ¤

ComponentFormatter ¤

ComponentFormatter(tag: str)

Bases: TagFormatterABC

The original django_component's component tag formatter, it uses the component and endcomponent tags, and the component name is gives as the first positional arg.

Example as block:

{% component "mycomp" abc=123 %}
    {% fill "myfill" %}
        ...
    {% endfill %}
{% endcomponent %}

Example as inlined tag:

{% component "mycomp" abc=123 / %}

Source code in src/django_components/tag_formatter.py
def __init__(self, tag: str):
    self.tag = tag

InternalTagFormatter ¤

InternalTagFormatter(tag_formatter: TagFormatterABC)

Internal wrapper around user-provided TagFormatters, so that we validate the outputs.

Source code in src/django_components/tag_formatter.py
def __init__(self, tag_formatter: TagFormatterABC):
    self.tag_formatter = tag_formatter

ShorthandComponentFormatter ¤

Bases: TagFormatterABC

The component tag formatter that uses <name> / end<name> tags.

This is similar to django-web-components and django-slippers syntax.

Example as block:

{% mycomp abc=123 %}
    {% fill "myfill" %}
        ...
    {% endfill %}
{% endmycomp %}

Example as inlined tag:

{% mycomp abc=123 / %}

TagFormatterABC ¤

Bases: ABC

end_tag abstractmethod ¤

end_tag(name: str) -> str

Formats the end tag of a block component.

Source code in src/django_components/tag_formatter.py
@abc.abstractmethod
def end_tag(self, name: str) -> str:
    """Formats the end tag of a block component."""
    ...

parse abstractmethod ¤

parse(tokens: List[str]) -> TagResult

Given the tokens (words) of a component start tag, this function extracts the component name from the tokens list, and returns TagResult, which is a tuple of (component_name, remaining_tokens).

Example:

Given a component declarations:

{% component "my_comp" key=val key2=val2 %}

This function receives a list of tokens

['component', '"my_comp"', 'key=val', 'key2=val2']

component is the tag name, which we drop. "my_comp" is the component name, but we must remove the extra quotes. And we pass remaining tokens unmodified, as that's the input to the component.

So in the end, we return a tuple:

('my_comp', ['key=val', 'key2=val2'])

Source code in src/django_components/tag_formatter.py
@abc.abstractmethod
def parse(self, tokens: List[str]) -> TagResult:
    """
    Given the tokens (words) of a component start tag, this function extracts
    the component name from the tokens list, and returns `TagResult`, which
    is a tuple of `(component_name, remaining_tokens)`.

    Example:

    Given a component declarations:

    `{% component "my_comp" key=val key2=val2 %}`

    This function receives a list of tokens

    `['component', '"my_comp"', 'key=val', 'key2=val2']`

    `component` is the tag name, which we drop. `"my_comp"` is the component name,
    but we must remove the extra quotes. And we pass remaining tokens unmodified,
    as that's the input to the component.

    So in the end, we return a tuple:

    `('my_comp', ['key=val', 'key2=val2'])`
    """
    ...

start_tag abstractmethod ¤

start_tag(name: str) -> str

Formats the start tag of a component.

Source code in src/django_components/tag_formatter.py
@abc.abstractmethod
def start_tag(self, name: str) -> str:
    """Formats the start tag of a component."""
    ...

TagResult ¤

Bases: NamedTuple

The return value from TagFormatter.parse()

component_name instance-attribute ¤

component_name: str

Component name extracted from the template tag

tokens instance-attribute ¤

tokens: List[str]

Remaining tokens (words) that were passed to the tag, with component name removed

get_tag_formatter ¤

get_tag_formatter(registry: ComponentRegistry) -> InternalTagFormatter

Returns an instance of the currently configured component tag formatter.

Source code in src/django_components/tag_formatter.py
def get_tag_formatter(registry: "ComponentRegistry") -> InternalTagFormatter:
    """Returns an instance of the currently configured component tag formatter."""
    # Allow users to configure the component TagFormatter
    formatter_cls_or_str = registry.settings.TAG_FORMATTER

    if isinstance(formatter_cls_or_str, str):
        tag_formatter: TagFormatterABC = import_string(formatter_cls_or_str)
    else:
        tag_formatter = formatter_cls_or_str

    return InternalTagFormatter(tag_formatter)