References
kTemplate.main
attr2str(key: str, attrs: dict) -> str
create attribute string of specific key in an html element
Examples:
Here shows the attribute string output from diff types of attribute dict value
- str -> str
- empty str -> empty str
- non str truthy -> key itself
- non str falsy -> omit
>>> attr2str(key="x", attrs={"x": False})
''
>>> attr2str(key="x", attrs={"x": None})
''
>>> attr2str(key="x", attrs={"x": 0})
''
- convert attr underscore to hyphen
- convert attr name cls to class
Note
to work around python naming restriction,
the key cls
will conver to class
,
and underscore _
will convert to hyphen -
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
attribute key name |
required |
attrs |
dict
|
attribute key-value pairs of an element |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
attribute portion of an element |
Source code in kTemplate/main.py
create_elements(tags: str) -> list[Callable[..., str]]
create tagged element functions
Notes
This is a higher order function that returns a list of functions
Examples:
- single element
- multiple elements
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tags |
str
|
names of functions to be created, comma separated eg. "a, br, div, span" |
required |
Returns:
Type | Description |
---|---|
list[Callable[..., str]]
|
list[TaggedElement]: list of tagged element functions eg. [a br div span] |
Source code in kTemplate/main.py
element(tag: str, content: str | list[str] = None, *args, **kwargs) -> str
return html element with specific tag and attributes
Examples:
- void element, content=None (default)
- void element w/ attr
- empty string content -> element with end tag but no content
- non-string truthy attrubite -> return attribute key itself
- non-string falsy attrubite -> attribute omitted
- var positional args -> return attribute key itself
- var positional args, useful in UnoCSS attributify mode
- var positional args + keyword args
>>> element('a', 'foo', 'm-2', 'rounded', 'text-teal-400', href='bar')
'<a href="bar" m-2 rounded text-teal-400>foo</a>'
- element tree
- mix text w/ element
- content w/ list of elements -> elements in list are siblings
>>> element(
... tag="div",
... content=[element("br"), element("a", content="a link", href="url")]
... )
'<div><br /><a href="url">a link</a></div>'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tag |
str
|
element tag name |
required |
content |
str | list[str]
|
Defaults to None.
text or list of other elements, |
None
|
args |
list[str]
|
names of value-less attributes
- eg. |
()
|
kwagrs |
dict
|
key-value pairs of html attributes
- if val is str, assign |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
html element with specific tag and attributes |