dj_angles.replacers.attributes

Replace attributes like <div dj-if=”condition”> with Django template tags.

  1. Parses HTML into a tree structure

  2. Groups conditional elements by their parent (siblings)

  3. Chains siblings together (if → elif → else)

  4. Generates output with proper {% if %}/{% endif %} wrapping

Module Contents

class dj_angles.replacers.attributes.Element

Represents an HTML element with a dj-* attribute.

tag_name: str

Name of the HTML tag, e.g. div or img.

tag_start: int

Position of the < that starts the opening tag in the original HTML.

tag_end: int

Position just after the > that ends the opening tag.

full_end: int

Position just after the element’s closing tag, or tag_end for void/self-closing tags.

original_tag: str

The original opening tag string, including the dj-* attribute.

original_full: str

The full original element, from the opening tag through the closing tag.

attr_match: re.Match

Regex match for the dj-* attribute that triggered this element’s discovery.

type: str

The dj-* attribute type, e.g. if, elif, else, or value.

value: str = ''

The value of the dj-* attribute, e.g. the condition for dj-if or the variable for dj-value.

property is_closing: bool

Whether the element is a closing tag.

remove_attribute() str

Remove the dj-* attribute from the opening tag.

contains(other: Element) bool

Whether this element fully contains another element.

closing_tag() str

Return the existing closing tag or generate one.

class dj_angles.replacers.attributes.ConditionalElement

Bases: Element

Represents an element with a dj-if/elif/else attribute.

chain_id: int
next_in_chain: ConditionalElement | None = None
property condition: str

The condition value of the attribute (empty for else).

dj_angles.replacers.attributes.replace_conditionals(html: str) str

Convert dj-if/elif/else attributes to Django template tags.

Parameters:
  • html – The HTML string to process

  • prefix – The attribute prefix (default “dj-“). If None, uses detailed configuration.

Returns:

HTML with Django template tags

dj_angles.replacers.attributes.replace_values(html: str) str

Convert dj-value attributes to Django variable output.

The attribute is removed from the opening tag, the element’s inner content is replaced with {{ value }}, and void/self-closing tags are turned into paired tags so the value has a place to render.

Parameters:

html – The HTML string to process.

Returns:

HTML with dj-value attributes replaced by Django template variables.