holm.utils
default_text_processor(text, context)
Default text processor for Snippet and MD components, that internally uses
str.format() to format text.
The standard str.format() syntax and escaping rules must be used in text,
see the documentation
for more information.
Supported replacement field in text:
metadata: TheMetadatainstance from thehtmycontext, which implements theMappingprotocol, so you can access its items using the[]operator. Example text:<title>"{metadata[title]}"</title>.request: The current FastAPIRequestfrom thehtmycontext. Example text:<base href="{request.url.origin}/">.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to process. |
required |
context
|
Context
|
The |
required |
Returns:
| Type | Description |
|---|---|
str
|
The formatted text. |
Source code in holm/utils.py
snippet_to_layout(content, *, text_processor=default_text_processor, default_slot_mapping=None)
Converts the given string to a Layout.
The created layout function internally uses a Snippet component with Slots for rendering.
The slot handling logic depends on the "props" object the layout receives from the page or layout it directly wraps:
- If "props" is a
Mapping(and not anhtmyComponent), then it must map slot keys to the correspondinghtmycomponents, and it is used directly as the slots definition ofSnippet. This means<!-- slot[key] -->placeholders incontentare replaced with the corresponding components from this mapping during rendering. - Otherwise the props object is assumed to be an
htmyComponentand it is assigned to thechildrenslot, meaning the<!-- slot[children] -->placeholder incontentis replaced with this component during rendering.
The default text processor is holm.utils.default_text_processor(). It gives you access to
the page Metadata and the current FastAPI Request through the metadata and request
replacement field names. See its documentation for more details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
The string to convert. |
required |
text_processor
|
TextProcessor
|
The text processor for |
default_text_processor
|
default_slot_mapping
|
Mapping[str, Component] | None
|
Default slots that should always be available in the layout during rendering. |
None
|
Returns:
| Type | Description |
|---|---|
Layout
|
A |