class Tag
from dj_angles.tags import Tag
Encapsulates metadata and functionality for a tag that will be processed by `dj-angles`.
Ancestors (MRO)
Attribute | Type | Defined in |
---|---|---|
__dict__ |
getset_descriptor
|
dj_angles.tags.Tag |
__weakref__ |
getset_descriptor
|
dj_angles.tags.Tag |
Attribute | Value | Defined in |
---|---|---|
__annotations__ |
{'tag_name': <class 'str'>, 'html': <class 'str'>, 'attributes': <class 'dj_angles.attributes.Attributes'>, 'is_shadow': <class 'bool'>, 'is_end': <class 'bool'>, 'is_self_closing': <class 'bool'>, 'start_tag': typing.Optional[ForwardRef('Tag')]} |
dj_angles.tags.Tag |
is_end |
False |
dj_angles.tags.Tag |
is_self_closing |
False |
dj_angles.tags.Tag |
is_shadow |
False |
dj_angles.tags.Tag |
start_tag |
None |
dj_angles.tags.Tag |
def __init__(self)
dj_angles.tags.Tag
Initialize self. See help(type(self)) for accurate signature.
def __init__(
self,
tag_map: Optional[dict[Optional[str], Union[Callable, str]]],
html: str,
tag_name: str,
template_tag_args: str,
tag_queue: Optional["deque"] = None,
):
self.html = html
self.tag_name = tag_name
self._template_tag_args = template_tag_args
self.parse_attributes()
if self.tag_name.endswith("!"):
self.tag_name = self.tag_name[:-1]
self.is_shadow = True
else:
shadow_attribute = self.attributes.get("shadow")
if shadow_attribute:
self.is_shadow = True
self.attributes.remove(shadow_attribute.key)
if get_setting("lower_case_tag", default=False):
self.tag_name = self.tag_name.lower()
if tag_map is None:
raise AssertionError("Invalid tag_map")
# Get the Django template tag based on the tag name or get the fallback with magic `None`
self.django_template_tag = tag_map.get(self.tag_name) or tag_map.get(None)
self.is_self_closing = self.html.endswith("/>")
self.is_end = self.html.startswith("</")
if self.is_end and tag_queue:
# Assume that the last tag before this end tag was the related start tag
self.start_tag = tag_queue[-1]
def __str__(self)
dj_angles.tags.Tag
Return str(self).
def __str__(self):
return self.html
def component_name(self)
dj_angles.tags.Tag
Legacy property for `tag_name`. Deprecated.
def get_django_template_tag(self)
dj_angles.tags.Tag
Generate the Django template tag. Args: param slots: List of slots which is a tuple of slot name and inner html.
def get_django_template_tag(self, slots: Optional[list[tuple[str, Element]]] = None) -> str:
"""Generate the Django template tag.
Args:
param slots: List of slots which is a tuple of slot name and inner html.
"""
if slots and self.is_include:
self.django_template_tag = map_angles_include
self.slots = slots
if callable(self.django_template_tag):
return str(
self.django_template_tag(
tag=self,
)
)
if self.is_end:
self.django_template_tag = f"end{self.django_template_tag}"
if self.attributes:
return f"{{% {self.django_template_tag} {self.attributes} %}}"
return f"{{% {self.django_template_tag} %}}"
def get_wrapping_tag_name(self)
dj_angles.tags.Tag
Get the wrapping tag name. Args: param name: The name for the wrapping tag.
def get_wrapping_tag_name(self, name: Optional[str] = None) -> str:
"""Get the wrapping tag name.
Args:
param name: The name for the wrapping tag.
"""
name = name or self.tag_name
wrapping_tag_name = (
name.replace("/", "-")
.replace("'", "")
.replace('"', "")
.replace("--", "-")
.replace(" ", "-")
.replace(":", "-")
).lower()
wrapping_tag_name = f"dj-{wrapping_tag_name}"
# Remove extensions
if "." in wrapping_tag_name:
extension_idx = wrapping_tag_name.index(".")
wrapping_tag_name = wrapping_tag_name[0:extension_idx]
# Remove shadow bang
if wrapping_tag_name.endswith("!"):
wrapping_tag_name = wrapping_tag_name[:-1]
return wrapping_tag_name
def is_include(self)
dj_angles.tags.Tag
Whether the Django template tag is `include`.
def parse_attributes(self)
dj_angles.tags.Tag
Creates `Attributes` based on the template tag arguments.
def parse_attributes(self):
"""Creates `Attributes` based on the template tag arguments."""
self.attributes = Attributes(self._template_tag_args)