Templating¶
Jinja2-based template engine for Gemtext output.
TemplateEngine¶
High-level template rendering interface.
TemplateEngine
¶
High-level template rendering interface.
Example
engine = TemplateEngine(Path("templates")) response = engine.render("page.gmi", title="Welcome", items=["a", "b"])
With app integration (enables reverse() in templates): engine = TemplateEngine(Path("templates"), app=app) # In templates: # {{ reverse("user_profile", username="alice") | link("Profile") }}
Create a template engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
templates_dir
|
Path
|
Directory containing template files. |
required |
app
|
Xitzin | None
|
Optional Xitzin app instance for URL reversing in templates. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If templates_dir doesn't exist. |
Source code in src/xitzin/templating.py
render
¶
Render a template file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template_name
|
str
|
Name of the template file (e.g., "page.gmi"). |
required |
**context
|
Any
|
Variables to pass to the template. |
{}
|
Returns:
| Type | Description |
|---|---|
TemplateResponse
|
TemplateResponse that can be returned from handlers. |
Example
return engine.render("user.gmi", username="alice", posts=posts)
Source code in src/xitzin/templating.py
render_string
¶
Render a template from a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str
|
Template source string. |
required |
**context
|
Any
|
Variables to pass to the template. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
Rendered string. |
Example
result = engine.render_string("# {{ title }}", title="Hello")
Source code in src/xitzin/templating.py
TemplateResponse¶
Response wrapper for rendered templates.
TemplateResponse
¶
Gemtext Filters¶
The template engine includes these filters for generating Gemtext:
link¶
Generate a Gemtext link line.
{{ "/about" | link("About Us") }}
{# Output: => /about About Us #}
{{ "/home" | link }}
{# Output: => /home #}
heading¶
Generate a Gemtext heading (levels 1-3).
{{ "Title" | heading(1) }}
{# Output: # Title #}
{{ "Section" | heading(2) }}
{# Output: ## Section #}
{{ "Subsection" | heading(3) }}
{# Output: ### Subsection #}
list¶
Generate a Gemtext list from an iterable.
quote¶
Generate a Gemtext blockquote.
preformat¶
Generate a preformatted code block.
code}¶
```