### Hello, World! Example
Source: https://github.com/aremeis/dominix/blob/master/README.md
A minimal example demonstrating how to create a simple HTML structure with a heading using Dominix.
```python
from dominix.tags import *
print(html(body(h1('Hello, World!'))))
```
--------------------------------
### Hello, World! HTML Output
Source: https://github.com/aremeis/dominix/blob/master/README.md
The HTML output for the 'Hello, World!' Dominix example.
```html
Hello, World!
```
--------------------------------
### Dominix HTML Output
Source: https://github.com/aremeis/dominix/blob/master/README.md
The resulting HTML output generated from the basic Dominix document creation example, showcasing the structured HTML document.
```html
Dominix for president!
```
--------------------------------
### Create a Simple Document Tree
Source: https://github.com/aremeis/dominix/blob/master/README.md
Builds a basic HTML document structure with header, content, and footer divs using the `.add()` method.
```python
_html = html()
_body = _html.add(body())
header = _body.add(div(id='header'))
content = _body.add(div(id='content'))
footer = _body.add(div(id='footer'))
print(_html)
```
```html
```
--------------------------------
### Basic Document Creation with Dominix
Source: https://github.com/aremeis/dominix/blob/master/README.md
Demonstrates the creation of a basic HTML document structure using Dominix, including head elements like stylesheets and scripts, and body content with nested tags and attributes.
```python
import dominix
from dominix.tags import *
doc = dominix.document(title='Dominix for president!')
with doc.head:
link(rel='stylesheet', href='style.css')
script(type='text/javascript', src='script.js')
with doc:
with div(id='header').add(ol()):
for i in ['home', 'about', 'contact']:
li(a(i.title(), href='/%s.html' % i))
with div():
attr(cls='body')
p('Lorem ipsum..')
print(doc)
```
--------------------------------
### Create and Print a Simple Div
Source: https://github.com/aremeis/dominix/blob/master/README.md
Demonstrates the basic creation of a `div` element with a data attribute and printing its HTML representation.
```python
test = div(data_employee='101011')
print(test)
```
```html
```
--------------------------------
### Create a Simple Comment
Source: https://github.com/aremeis/dominix/blob/master/README.md
Shows how to create a basic HTML comment node.
```python
print(comment('BEGIN HEADER'))
```
```html
```
--------------------------------
### Creating a Basic HTML Document
Source: https://github.com/aremeis/dominix/blob/master/README.md
Demonstrates the creation of a basic HTML document structure using the `document` class. The default structure includes DOCTYPE, html, head with a title, and body tags.
```python
from dominix.document import document
d = document()
print(d)
```
--------------------------------
### Basic Widget Creation with Decorators
Source: https://github.com/aremeis/dominix/blob/master/README.md
Illustrates creating a reusable widget function using a tag as a decorator. The decorated function's output is wrapped in the specified tag. This simplifies boilerplate code for creating widgets.
```python
from dominix.tags import div, p
@div
def greeting(name):
p('Hello %s' % name)
print(greeting('Bob'))
```
--------------------------------
### Creating Paragraphs with Text and Links
Source: https://github.com/aremeis/dominix/blob/master/README.md
Demonstrates how to create a paragraph element with plain text and an anchor tag using dominix.util.text and the p tag. The output shows the generated HTML.
```python
from dominix.util import text
from dominix.tags import p, a
para = p(__pretty=False)
with para:
text('Have a look at our ')
a('other products', href='/products')
print(para)
```
--------------------------------
### Create a List with a Loop
Source: https://github.com/aremeis/dominix/blob/master/README.md
Illustrates creating an unordered list (`ul`) and populating it with list items (`li`) using a `for` loop and the `+=` operator.
```python
list = ul()
for item in range(4):
list += li('Item #', item)
print(list)
```
```html
Item #0
Item #1
Item #2
Item #3
```
--------------------------------
### Django Template vs. Dominix
Source: https://github.com/aremeis/dominix/blob/master/README.md
Compares a Django template snippet with its equivalent Python code using Dominix, demonstrating the library's ability to generate HTML from Python.
```html
{% if latest_question_list %}
{% endif %}
```
```python
if latest_question_list:
with ul():
for question in latest_question_list:
li(a(question.question_text, href=f"/polls/{question.id}/"))
else:
p("No polls are available.")
```
--------------------------------
### Widget Creation with Tag Instances as Decorators
Source: https://github.com/aremeis/dominix/blob/master/README.md
Shows how to use an instance of a tag as a decorator to add attributes and child nodes to the root element of a widget. Each call to the decorated function returns a copy of the decorated tag instance.
```python
from dominix.tags import div, p, h2
@div(h2('Welcome'), cls='greeting')
def greeting(name):
p('Hello %s' % name)
print(greeting('Bob'))
```
--------------------------------
### Create List from Iterable
Source: https://github.com/aremeis/dominix/blob/master/README.md
Demonstrates creating an unordered list (`ul`) from an iterable of menu items using a generator expression.
```python
menu_items = [
('Home', '/home/'),
('About', '/about/'),
('Downloads', '/downloads/'),
('Links', '/links/')
]
print(ul(li(a(name, href=link), __pretty=False) for name, link in menu_items))
```
```html
```
--------------------------------
### Creating SVG Elements
Source: https://github.com/aremeis/dominix/blob/master/README.md
Demonstrates the creation of SVG elements using the `dominix.svg` module. SVG tags automatically handle attribute name conversions, such as replacing underscores with hyphens for dashed attributes.
```python
from dominix.svg import *
print(circle(stroke_width=5))
```
--------------------------------
### Dominix Attribute Aliases
Source: https://github.com/aremeis/dominix/blob/master/README.md
Illustrates the use of Dominix's attribute aliases for Python keywords like 'class' and 'for', showing how to map them to HTML attributes.
```python
test = label(cls='classname anothername', fr='someinput')
print(test)
```
--------------------------------
### Accessing Document Components
Source: https://github.com/aremeis/dominix/blob/master/README.md
Shows how to access the head, body, and title components of a document object created with `dominix.document.document`. This allows direct manipulation or inspection of these parts.
```python
from dominix.document import document
d = document()
print(d.head)
print(d.body)
print(d.title)
```
--------------------------------
### Use Context Manager to Add List Items
Source: https://github.com/aremeis/dominix/blob/master/README.md
Utilizes a Python `with` statement to add list items (`li`) to an unordered list (`ul`).
```python
h = ul()
with h:
li('One')
li('Two')
li('Three')
print(h)
```
```html
One
Two
Three
```
--------------------------------
### Cleaned Document Tree Creation
Source: https://github.com/aremeis/dominix/blob/master/README.md
A more concise way to create a document tree, including a title in the head, and using chained `.add()` calls for body elements.
```python
_html = html()
_head, _body = _html.add(head(title('Simple Document Tree')), body())
names = ['header', 'content', 'footer']
header, content, footer = _body.add([div(id=name) for name in names])
print(_html)
```
```html
Simple Document Tree
```
--------------------------------
### Complex Nested Structure with Context Managers
Source: https://github.com/aremeis/dominix/blob/master/README.md
Builds a complex HTML document including nested divs, headings, paragraphs, tables, and rows using multiple context managers and the `.add()` method.
```python
h = html()
with h.add(body()).add(div(id='content')):
h1('Hello World!')
p('Lorem ipsum ...')
with table().add(tbody()):
l = tr()
l += td('One')
l.add(td('Two'))
with l:
td('Three')
print(h)
```
```html
Hello World!
Lorem ipsum ...
One
Two
Three
```
--------------------------------
### Dominix Attribute Aliases HTML Output
Source: https://github.com/aremeis/dominix/blob/master/README.md
The HTML output demonstrating the use of Dominix's attribute aliases for 'class' and 'for'.
```html
```
--------------------------------
### Using 'attr' for Method Chaining
Source: https://github.com/aremeis/dominix/blob/master/features.md
Demonstrates the use of the 'attr' function and method in Dominix for adding attributes, supporting method chaining.
```python
from dominix.tags import div, h1
def section(title):
return div(h1(title))
def my_section():
return section("Chained").attr(hx_get="/my/endpoint")
```
--------------------------------
### Basic Section Generation
Source: https://github.com/aremeis/dominix/blob/master/features.md
Demonstrates creating a section tag with an HTMX attribute and rendering it to HTML. This is an alternative to method chaining.
```python
def my_section():
tag = section("Chained")
with tag:
attr(hx_get="/my/endpoint")
return tag
```
--------------------------------
### Add Attributes within Context
Source: https://github.com/aremeis/dominix/blob/master/README.md
Demonstrates adding attributes to the current context using the `attr` function within a `with` statement.
```python
d = div()
with d:
attr(id='header')
print(d)
```
```html
```
--------------------------------
### Embedding Raw HTML
Source: https://github.com/aremeis/dominix/blob/master/README.md
Explains how to use `dominix.util.raw` to embed pre-formatted HTML content without it being escaped. This is useful when integrating with other libraries that generate HTML.
```python
from dominix.util import raw
from dominix.tags import td
# Example usage within a table cell
# print(td(raw('Example')))
```
--------------------------------
### Alpine.js x-on Attribute
Source: https://github.com/aremeis/dominix/blob/master/features.md
Shows different ways to implement the `x-on` attribute in Dominix for handling events like clicks, mirroring the HTML structure.
```python
# Alternative 1
tag = div(x_on={"click": "alert()"})
# Alternative 2
tag = div()
tag.x_on["click"] = "alert()"
# Alternative 3
tag = div(x_on_click="alert()")
```
--------------------------------
### Alpine.js x-bind Attribute
Source: https://github.com/aremeis/dominix/blob/master/features.md
Demonstrates various methods for implementing the `x-bind` attribute in Dominix, used for binding data to HTML attributes like 'placeholder'.
```python
# Alternative 1
tag = div(x_bind={"placeholder": "foo"})
# Alternative 2
tag = div()
tag.x_bind["placeholder"] = "foo"
# Alternative 3
tag = div(x_bind_placeholder="foo")
```
--------------------------------
### Create a Conditional Comment
Source: https://github.com/aremeis/dominix/blob/master/README.md
Demonstrates creating an HTML conditional comment (IE specific) that wraps a paragraph element.
```python
print(comment(p('Upgrade to newer IE!'), condition='lt IE9'))
```
```html
```
--------------------------------
### HTMX Attribute Usage
Source: https://github.com/aremeis/dominix/blob/master/features.md
Demonstrates how to use HTMX attributes like hx-get in Dominix, showing both constructor argument and property assignment for tags.
```python
from dominix.tags import div
tag = div(hx_get="/my/endpoint")
print(tag)
tag = div()
tag.hx_get = "/my/endpoint"
print(tag)
```
```html
```
--------------------------------
### Render Element with `__pretty=False`
Source: https://github.com/aremeis/dominix/blob/master/README.md
Demonstrates that the `__pretty=False` attribute during element creation overrides default rendering behavior.
```python
a = div(span('Hello World'), __pretty=False)
print(a.render())
```
```html
Hello World
```
--------------------------------
### Render Element with Default Pretty Printing
Source: https://github.com/aremeis/dominix/blob/master/README.md
Renders an HTML element with default indentation and newlines for readability.
```python
a = div(span('Hello World'))
print(a.render())
```
```html
Hello World
```
--------------------------------
### Special Attribute hx-on
Source: https://github.com/aremeis/dominix/blob/master/features.md
Illustrates the special implementation of the 'hx-on' attribute in Dominix, allowing event handlers to be managed via a dictionary property or constructor argument.
```html
Click
```
```python
from dominix.tags import div
tag = div()
tag.hx_on["click"] = "alert()"
tag = div(hx_on={"click": "alert('Clicked')"})
```
```html
```
--------------------------------
### Render Element with Tab Indentation
Source: https://github.com/aremeis/dominix/blob/master/README.md
Renders an HTML element using tabs for indentation.
```python
print(a.render(indent='\t'))
```
```html
Hello World
```
--------------------------------
### Render Element with Mixed Content and XHTML
Source: https://github.com/aremeis/dominix/blob/master/README.md
Renders a div containing a horizontal rule, paragraph, and line break, showing default and XHTML output.
```python
d = div()
with d:
hr()
p("Test")
br()
print(d.render())
print(d.render(xhtml=True))
```
```html
Test
Test
```
--------------------------------
### Alpine.js Attribute Mapping
Source: https://github.com/aremeis/dominix/blob/master/features.md
Illustrates how Dominix maps Alpine.js attributes, including those with special characters like '-', '.', or ':', to Python arguments using underscores.
```APIDOC
Alpine.js Attribute Mapping:
- `x-bind:placeholder` -> `x_bind_placeholder`
- `x-model:lazy` -> `x_model_lazy`
- `x-transition:enter-start` -> `x_transition_enter_start`
- `x-transition:enter.scale.80` -> `x_transition_enter_scale_80`
```
--------------------------------
### hx-val and hx-headers Attributes
Source: https://github.com/aremeis/dominix/blob/master/features.md
Shows how 'hx-val' and 'hx-headers' attributes are handled in Dominix, converting Python dictionaries to JSON strings for HTMX.
```python
from dominix.tags import div
tag = div(hx_headers={"foo": "bar", "baz": "qux"})
tag.hx_vals = {"foo": "bar", "baz": "qux"}
```
```html
```
--------------------------------
### Adding Nodes to the Document Body
Source: https://github.com/aremeis/dominix/blob/master/README.md
Illustrates how to add elements like H1 and P directly to the body of a document object using the `+=` operator. This provides a convenient way to build the document's content.
```python
from dominix.document import document
from dominix.tags import h1, p
d = document()
d += h1('Hello, World!')
d += p('This is a paragraph.')
print(d)
```
--------------------------------
### Style Attribute Manipulation with 'style'
Source: https://github.com/aremeis/dominix/blob/master/features.md
Explains the 'style' property in Dominix for managing the HTML 'style' attribute using Python dictionary operations.
```python
from dominix.tags import div
tag = div(style="color: red; font-size: 12px")
tag.style["color"] = "blue"
del tag.style["font-size"]
tag.style["font-color"] = "green"
```
```html
```
--------------------------------
### Modify Div Attributes
Source: https://github.com/aremeis/dominix/blob/master/README.md
Shows how to modify the attributes of an existing HTML element using dictionary-like access.
```python
header = div()
header['id'] = 'header'
print(header)
```
```html
```
--------------------------------
### Method Chaining for Attribute Manipulation
Source: https://github.com/aremeis/dominix/blob/master/features.md
Showcases method chaining in Dominix using convenience methods like add_class, rem_class, upd_style, del_style, upd_hx_on, del_hx_on, upd_hx_vals, del_hx_vals, upd_hx_headers, and del_hx_headers.
```python
from dominix.tags import div, hx_on, add_class, rem_class, upd_hx_on, del_hx_on
def component():
return div("Hello", cls="one two three", hx_on={"click": "alert('click')"})
def my_component():
return component().add_class("four").rem_class("one").upd_hx_on("mouseover", "alert('mouseover')").del_hx_on("click")
# Using with syntax:
def my_component_with():
with component() as c:
add_class("four")
rem_class("one")
upd_hx_on("mouseover", "alert('mouseover')")
del_hx_on("click")
return c
```
```html
Hello
```
--------------------------------
### Render Element Without Pretty Printing
Source: https://github.com/aremeis/dominix/blob/master/README.md
Renders an HTML element as a single line without extra whitespace.
```python
print(a.render(pretty=False))
```
```html
Hello World
```
--------------------------------
### Modify Element Children
Source: https://github.com/aremeis/dominix/blob/master/README.md
Demonstrates modifying the content of an element using array-like indexing.
```python
header = div('Test')
header[0] = 'Hello World'
print(header)
```
```html
Hello World
```
--------------------------------
### Class Attribute Manipulation with 'cls'
Source: https://github.com/aremeis/dominix/blob/master/features.md
Demonstrates the 'cls' property in Dominix for easy manipulation of the HTML 'class' attribute using Python list operations.
```python
from dominix.tags import div
tag = div(cls="a b c")
tag.cls.extend(["d", "e", "f"])
tag.cls.append("g")
tag.cls.remove("b")
```
```html
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.