class Tag

from dj_angles.tags import Tag
Encapsulates metadata and functionality for a tag that will be processed by `dj-angles`.

Ancestors (MRO)

  1. builtins.object
  2. dj_angles.tags.Tag
AttributeTypeDefined in
AttributeValueDefined in
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)