tag_formatter Β€
Classes:
-
ComponentFormatter
βThe original django_component's component tag formatter, it uses the
{% component %}
-
InternalTagFormatter
βInternal wrapper around user-provided TagFormatters, so that we validate the outputs.
-
ShorthandComponentFormatter
βThe component tag formatter that uses
{% <name> %}
/{% end<name> %}
tags. -
TagFormatterABC
βAbstract base class for defining custom tag formatters.
-
TagResult
βThe return value from
TagFormatter.parse()
.
Functions:
-
get_tag_formatter
βReturns an instance of the currently configured component 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 given as the first positional arg.
Example as block:
Example as inlined tag:
Source code in src/django_components/tag_formatter.py
InternalTagFormatter Β€
InternalTagFormatter(tag_formatter: TagFormatterABC)
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:
Example as inlined tag:
TagFormatterABC Β€
Bases: ABC
Abstract base class for defining custom tag formatters.
Tag formatters define how the component tags are used in the template.
Read more about Tag formatter.
For example, with the default tag formatter (ComponentFormatter
), components are written as:
While with the shorthand tag formatter (ShorthandComponentFormatter
), components are written as:
Example:
Implementation for ShorthandComponentFormatter
:
from djagno_components import TagFormatterABC, TagResult
class ShorthandComponentFormatter(TagFormatterABC):
def start_tag(self, name: str) -> str:
return name
def end_tag(self, name: str) -> str:
return f"end{name}"
def parse(self, tokens: List[str]) -> TagResult:
tokens = [*tokens]
name = tokens.pop(0)
return TagResult(name, tokens)
Methods:
-
end_tag
βFormats the end tag of a block component.
-
parse
βGiven the tokens (words) passed to a component start tag, this function extracts
-
start_tag
βFormats the start tag of a component.
end_tag abstractmethod
Β€
parse abstractmethod
Β€
Given the tokens (words) passed to 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)
.
Parameters:
-
tokens
([List(str]
) βList of tokens passed to the component tag.
Returns:
-
TagResult
(TagResult
) βParsed component name and remaining tokens.
Example:
Assuming we used a component in a template like this:
This function receives a list of tokens:
component
is the tag name, which we drop."my_comp"
is the component name, but we must remove the extra quotes.- The remaining tokens we pass unmodified, as that's the input to the component.
So in the end, we return:
Source code in src/django_components/tag_formatter.py
TagResult Β€
Bases: NamedTuple
The return value from TagFormatter.parse()
.
Read more about Tag formatter.
Attributes:
-
component_name
(str
) βComponent name extracted from the template tag
-
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.