Jinax API
Jinax
Jinax template engine.
Constructor
Jinax(template_directory: str | Path = "templates", *, auto_reload: bool = False, strict_undefined: bool = True, globals: dict[str, Any] | None = None, filters: dict[str, Callable[..., Any]] | None = None)
Methods
add_global
def add_global(name: str, value: Any) -> None
Add a global variable.
add_filter
def add_filter(name: str, func: Callable[..., Any]) -> None
Add a custom filter.
render
async def render(template_name: str, context: dict[str, Any] | None = None) -> str
Render a template.
render_response
async def render_response(template_name: str, context: dict[str, Any] | None = None, *, status_code: int = 200, headers: dict[str, str] | None = None) -> HTMLResponse
Render and return an HTML response.
Environment
Jinax environment.
Constructor
Environment(loader: Any, autoescape: bool = True, enable_async: bool = True, auto_reload: bool = False, strict_undefined: bool = False)
Methods
add_global(name, value)— Add a global variableadd_filter(name, func)— Add a custom filterget_template(name)— Get a templatefrom_string(source)— Create a template from a string
Loader
Template loader.
Constructor
Loader(search_path: str | Path, encoding: str = "utf-8")
Methods
get_source(environment, template)— Get template sourcelist_templates()— List all templates
Filters
Built-in Filters
| Filter | Description |
|---|---|
capitalize | Capitalize a string |
lower | Convert to lowercase |
upper | Convert to uppercase |
title | Convert to title case |
trim | Trim whitespace |
escape | Escape HTML |
safe | Mark as safe |
json | Convert to JSON |
length | Get length |
reverse | Reverse a string or list |
join | Join a list |
replace | Replace text |
date | Format a date |
datetime | Format a datetime |
currency | Format as currency |
truncate | Truncate text |
default | Default value |
first | First item |
last | Last item |
Custom Filter Example
def currency_filter(value, symbol="$"):
return f"{symbol}{value:.2f}"
jinax.add_filter("currency", currency_filter)
Functions
Built-in Functions
now()— Current datetimedate()— Current datedatetime()— Current datetimerange(n)— Range of numberslength(value)— Get lengthtype(value)— Get typejson(value)— Convert to JSONrandom()— Random floatrandom_int(min, max)— Random integerrandom_choice(items)— Random choiceuuid()— Generate UUIDhash(value)— Hash a valueenv(key, default)— Get environment variable
TemplateNotFound
Exception raised when a template is not found.
TemplateNotFound(template: str)
Tip
Jinax is lazily loaded. If you don't use templates, Jinja2 is never imported.