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 variable
  • add_filter(name, func) — Add a custom filter
  • get_template(name) — Get a template
  • from_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 source
  • list_templates() — List all templates

Filters

Built-in Filters

Filter Description
capitalizeCapitalize a string
lowerConvert to lowercase
upperConvert to uppercase
titleConvert to title case
trimTrim whitespace
escapeEscape HTML
safeMark as safe
jsonConvert to JSON
lengthGet length
reverseReverse a string or list
joinJoin a list
replaceReplace text
dateFormat a date
datetimeFormat a datetime
currencyFormat as currency
truncateTruncate text
defaultDefault value
firstFirst item
lastLast 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 datetime
  • date() — Current date
  • datetime() — Current datetime
  • range(n) — Range of numbers
  • length(value) — Get length
  • type(value) — Get type
  • json(value) — Convert to JSON
  • random() — Random float
  • random_int(min, max) — Random integer
  • random_choice(items) — Random choice
  • uuid() — Generate UUID
  • hash(value) — Hash a value
  • env(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.