# Templating

URL: /bengal/docs/build-sites/customize/templating/
Section: templating
Description: Kida template engine, layouts, inheritance, and partials

---

> For a complete page index, fetch /bengal/llms.txt.

Bengal's template system uses Kida (https://lbliii.github.io/kida/) as the default engine, with
support for Jinja2 and custom engines.

Note

Do I need this? Yes when writing or overriding HTML templates. Skip if you
only write Markdown — see Authoring (/bengal/docs/build-sites/write/authoring/). New to templates?
Start with Themer Quickstart (/bengal/docs/get-started/quickstart-themer/).

Functions vs Filters Quick Reference

One-page guide to when to use functions vs filters

(/bengal/docs/build-sites/customize/templating/functions-quick-reference/)

Template Functions

Quick overview of filters and functions available in templates

(/bengal/docs/build-sites/customize/templating/functions/)

Image Processing

Resize, crop, and optimize images in templates

(/bengal/docs/build-sites/customize/templating/image-processing/)

Bring Your Own Template Engine

Implement a custom template engine with full access to Bengal's 200+ template functions, filters, and tests

(/bengal/docs/build-sites/customize/templating/custom-engine/)

## Template Engines

- Kida (https://lbliii.github.io/kida/) (local reference (/bengal/docs/reference/kida-syntax/)) — Bengal's native template engine (default). Unified`{% end %}`blocks, pattern matching, pipeline operators

- Jinja2 (https://jinja.palletsprojects.com/) — Industry-standard engine with excellent documentation and tooling

- Custom engines — Bring your own via the plugin API

Tip

Kida is Jinja2-compatible: Your existing Jinja2 templates work without changes. Use Kida-specific features (https://lbliii.github.io/kida/docs/syntax) incrementally.

## Template Lookup Order

flowchart LR
A[Page Request] --> B{templates/ ?}
B -->|Found| C[Use Your Template]
B -->|Not Found| D{Theme templates/ ?}
D -->|Found| E[Use Theme Template]
D -->|Not Found| F[Use Bengal Default]

Bengal searches: Your project → Theme → Bengal defaults

## Quick Start

- Basic Template (#)

- Base Layout (#)

- Partial (#)

kida
KIDA

```
{# templates/layouts/single.html #}
{% extends "baseof.html" %}

{% block content %}
<article>
  <h1>{{ page.title }}</h1>
  {{ page.content | safe }}
</article>
{% end %}
```

kida
KIDA

```
{# templates/baseof.html #}
<!DOCTYPE html>
<html>
<head>
  <title>{% block title %}{{ page.title }}{% end %}</title>
</head>
<body>
  {% block header %}{% include "./partials/header.html" %}{% end %}
  {% block content %}{% end %}
  {% block footer %}{% include "./partials/footer.html" %}{% end %}
</body>
</html>
```

kida
KIDA

```
{# templates/partials/header.html #}
<header>
  <nav>
    {% for item in site.menus.main %}
      <a href="{{ item.href }}">{{ item.title }}</a>
    {% end %}
  </nav>
</header>
```

## Key Concepts

Concept
Syntax
Purpose

Extends
`{% extends "base.html" %}`
Inherit from parent template

Block
`{% block name %}...{% end %}`
Replaceable section

Include
`{% include "./partial.html" %}`
Insert another template

Variable
`Templating`
Output a value

Filter
`{{ text \| truncate(100) }}`
Transform a value

Kida resolves`./` and `../`relative template paths from the current template.
For shared component libraries, configure`kida.template_aliases`and include
templates with`@alias/` prefixes such as `{% include "@components/card.html" %}`.

## Template Inheritance

flowchart TB
A["baseof.html<br/>(blocks: head, content, footer)"]
B["single.html<br/>(extends baseof)"]
C["list.html<br/>(extends baseof)"]
D["doc.html<br/>(extends single)"]

A --> B
A --> C
B --> D

## Kida Features

Kida is Bengal's default template engine:

- Unified syntax:`{% end %}` closes all blocks (no more `{% end %}`, `{% end %}`)

- Pattern matching:`{% match %}...{% case %}`for cleaner conditionals

- Pipeline operator:`|>`for readable filter chains

- Fragment caching: Built-in`{% cache %}`directive

- Jinja2 compatible: Existing Jinja2 templates work without changes

Kida Syntax Reference

Complete Kida syntax documentation

(/bengal/docs/reference/kida-syntax/)

Kida Tutorial

Learn Kida from scratch

(/bengal/docs/tutorials/theming/getting-started-with-kida/)

Kida How-Tos

Common Kida tasks and patterns

(/bengal/docs/build-sites/customize/templating/kida/)

Migrate from Jinja2

Convert Jinja2 templates to Kida

(/bengal/docs/build-sites/customize/templating/kida/migration/from-jinja/)

## Choose Your Engine

Kida is the default. To use a different engine, configure`bengal.toml` (or `config/_default/site.yaml`):

yaml
YAML

```
site:
  template_engine: jinja2  # Options: kida (default), jinja2, mako
```
### Custom Engines (BYOR)

Bring your own template engine via the protocol API. Your engine automatically gets all 80+ template functions if it satisfies the`TemplateEnvironment`protocol:

python
PYTHON

```
from bengal.rendering.engines import register_engine
from bengal.rendering.template_functions import register_all

class MyEngine:
    def __init__(self, site):
        self.site = site
        self.template_dirs = [site.root_path / "templates"]

        # Environment must have globals, filters, tests dicts
        self._env = MyEnvironment()

        # Automatically get all 80+ template functions!
        register_all(self._env, site)

    def render_template(self, name: str, context: dict) -> str:
        # Your implementation
        ...

register_engine("myengine", MyEngine)
```
Then configure:

yaml
YAML

```
site:
  template_engine: myengine
```
See Bring Your Own Template Engine (/bengal/docs/build-sites/customize/templating/custom-engine/) for the complete guide.

Tip

Override sparingly: You only need to create templates you want to customize. Use`bengal theme swizzle --template-path <template>`to copy a template for customization. Let the rest fall through to theme defaults.

## In This Section

Kida Template Engine (/bengal/docs/build-sites/customize/templating/kida/)

Template engine with unified block endings, pattern matching, pipelines, and automatic caching

5 pages

Bring Your Own Template Engine (/bengal/docs/build-sites/customize/templating/custom-engine/)

Implement a custom template engine with full access to Bengal's 200+ template functions, filters, and tests

Functions vs Filters Quick Reference (/bengal/docs/build-sites/customize/templating/functions-quick-reference/)

One-page guide to when to use functions vs filters

Image Processing (/bengal/docs/build-sites/customize/templating/image-processing/)

Resize, crop, and optimize images in templates

Template Functions (/bengal/docs/build-sites/customize/templating/functions/)

Quick overview of filters and functions available in templates

Related Pages

Assets (/bengal/docs/build-sites/customize/assets/)

CSS, JavaScript, images, and fonts

Related

Build Sites (/bengal/docs/build-sites/)

Write, structure, customize, and extend your Bengal site

Related

Theming (/bengal/docs/build-sites/customize/)

Templates, assets, and visual customization

Related

Template Cookbook (/bengal/docs/build-sites/customize/recipes/)

Common templating patterns and Bengal-specific features

Related

persona-themer (/bengal/tags/persona-themer/)
