htmy.snippet
Slots
Utility that resolves slots in a string input to components.
More technically, it splits a string into slot and non-slot parts, replaces the slot parts with the corresponding components (which may be component sequences) from the given slot mapping, and returns the resulting component sequence.
The default slot placeholder is a standard XML/HTML comment of the following form:
<!-- slot[slot-key] -->
. Any number of whitespaces (including 0) are allowed in
the placeholder, but the slot key must not contain any whitespaces. For details, see
Slots.slot_re
.
Besides the pre-defined regular expressions in Slots.slot_re
, any other regular
expression can be used to identify slots as long as it meets the requirements described
in Slots.slots_re
.
Implements: htmy.typing.TextResolver
Source code in htmy/snippet.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
slot_re
Slot regular expressions.
Requirements:
- The regular expression must have exactly one capturing group that captures the slot key.
Source code in htmy/snippet.py
default = square_bracket
class-attribute
instance-attribute
The default slot regular expression. Same as Slots.slot_re.square_bracket
.
parentheses = re.compile('<!-- *slot *\\( *([^( ]+) *\\) *-->')
class-attribute
instance-attribute
Slot regular expression that matches slots defined as follows: <!-- slot(slot-key) -->
.
The slot key must not contain any whitespaces and there must not be any additional text in the XML/HTML comment. Any number of whitespaces (including 0) are allowed around the parts of the slot placeholder.
square_bracket = re.compile('<!-- *slot *\\[ *([^[ ]+) *\\] *-->')
class-attribute
instance-attribute
Slot regular expression that matches slots defined as follows: <!-- slot[slot-key] -->
.
The slot key must not contain any whitespaces and there must not be any additional text in the XML/HTML comment. Any number of whitespaces (including 0) are allowed around the parts of the slot placeholder.
__init__(slot_mapping, *, slot_re=slot_re.default, not_found=None)
Initialization.
Slot regular expressions are used to find slot keys in strings, which are then replaced
with the corresponding component from the slot mapping. slot_re
must have exactly one
capturing group that captures the slot key. Slots.slot_re
contains some predefined slot
regular expressions, but any other regular expression can be used as long as it matches
the capturing group requirement above.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
slot_mapping
|
Mapping[str, Component]
|
Slot mapping the maps slot keys to the corresponding component. |
required |
slot_re
|
Pattern[str]
|
The slot regular expression that is used to find slot keys in strings. |
default
|
not_found
|
Component | None
|
The component that is used to replace slot keys that are not found in
|
None
|
Source code in htmy/snippet.py
_resolve_text(text)
Generator that yields the slot and non-slot parts of the given string in order.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to resolve. |
required |
Yields:
Type | Description |
---|---|
ComponentType
|
The slot and non-slot parts of the given string. |
Raises:
Type | Description |
---|---|
KeyError
|
If a slot key is not found in the slot mapping and |
Source code in htmy/snippet.py
resolve_text(text)
Resolves the given string into components using the instance's slot regular expression and slot mapping.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to resolve. |
required |
Returns:
Type | Description |
---|---|
Component
|
The component sequence the text resolves to. |
Raises:
Type | Description |
---|---|
KeyError
|
If a slot key is not found in the slot mapping and |
Source code in htmy/snippet.py
Snippet
Component that renders text, which may be asynchronously loaded from a file.
The entire snippet processing pipeline consists of the following steps:
- The text content is loaded from a file or passed directly as a
Text
instance. - The text content is processed by a
TextProcessor
if provided. - The processed text is converted into a component (may be component sequence)
by a
TextResolver
, for exampleSlots
. - Every
str
children (produced by the steps above) is converted into aSafeStr
for rendering.
The pipeline above is a bit abstract, so here are some usage notes:
- The text content of a snippet can be a Python format string template, in which case the
TextProcessor
can be a simple method that callsstr.format()
with the correct arguments. - Alternatively, a text processor can also be used to get only a substring -- commonly referred to as fragment in frameworks like Jinja -- of the original text.
- The text processor is applied before the text resolver, which makes it possible to insert
placeholders into the text (for example slots, like in this case:
..."{toolbar}...".format(toolbar="<!-- slot[toolbar] -->")
) that are then replaced with anyhtmy.Component
by theTextResolver
(for exampleSlots
). TextResolver
can return plainstr
values, it is not necessary for it to convert strings toSafeStr
to prevent unwanted escaping.
Example:
from datetime import date
from htmy import Snippet, Slots
def text_processor(text: str, context: Context) -> str:
return text.format(today=date.today())
snippet = Snippet(
"my-page.html",
text_processor=text_processor,
text_resolver=Slots(
{
"date-picker": MyDatePicker(class_="text-primary"),
"Toolbar": MyPageToolbar(active_page="home"),
...
}
),
)
In the above example, if my-page.html
contains a {today}
placeholder, it will be replaced
with the current date. If it contains a <!-- slot[toolbar] -->}
slot, then the MyPageToolbar
htmy
component instance will be rendered in its place, and the <!-- slot[date-picker] -->
slot
will be replaced with the MyDatePicker
component instance.
Source code in htmy/snippet.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|
__init__(path_or_text, text_resolver=None, *, text_processor=None)
Initialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_or_text
|
Text | str | Path
|
The path from where the content should be loaded or a |
required |
text_resolver
|
TextResolver | None
|
An optional |
None
|
text_processor
|
TextProcessor | None
|
An optional |
None
|
Source code in htmy/snippet.py
_get_text_content()
async
Returns the plain text content that should be rendered.
Source code in htmy/snippet.py
_render_text(text, context)
Render function that takes the text that must be rendered and the current rendering context, and returns the corresponding component.
htmy(context)
async
Renders the component.