Theme system
Table of contents
- Introduction
- How the parts fit
- Extending the public shell
- Loading stylesheets in order
- Styling with design tokens
- Filling the public shell’s slots
- Adding a skin
- Adding an entry to the main nav
- Reference
- See also
Introduction
A product’s look comes from its features, like everything else it has. A theme feature draws the public website, and a skin feature lays a palette and fonts over it. You change how a product looks by selecting features and setting values, not by rewriting CSS.
A public page names its shell and fills the content block. The theme draws the rest.
{% extends shell('public') %}
{% block content %}
<h1>{{ _('Upcoming events') }}</h1>
{% endblock %}
How the parts fit
| Part | Gives |
|---|---|
| The framework | The shells and shell(), the asset registry and its order, template hooks, the nav and menu registries, param() |
| A theme feature | The public layout (public_base.html), its base stylesheet, the context its templates read, and the settings a product sets its identity with |
| A skin feature | A palette and fonts for the theme, and a stylesheet that loads last |
| The product | Which theme and skin it selects, and the values it sets in [tool.splent.config] |
| Content features | Their pages, their nav entries and what they put in the shell’s slots |
Extending the public shell
shell('public') resolves to public_base.html, the layout the theme ships. A product points its website at a layout of its own with SPLENT_SHELL_PUBLIC, and no page changes.
[tool.splent.config]
SPLENT_SHELL_PUBLIC = "my_site_base.html"
The shell never hardcodes a product name, a palette or a font. Layouts by shell covers every shell and its setting.
Loading stylesheets in order
def init_feature(app):
from splent_framework.assets.asset_registry import register_asset
register_asset(
"css", "my_theme.assets", order=0, subfolder="css", filename="public.css"
)
Every feature registers its stylesheets and scripts, and the shell loads them by order, lowest first. A skin then overrides the base look without editing it.
| Order | By convention |
|---|---|
Around 0 |
The theme’s base stylesheet |
Around 100 |
Each feature’s stylesheet, the default |
Around 200 |
A skin, which wins last |
shell= says which shell loads the file. See Assets per shell.
Styling with design tokens
.btn-primary {
background: var(--brand-primary);
color: var(--brand-primary-contrast);
}
A theme writes its palette, fonts and radii as CSS custom properties in <head>, and its stylesheet reads every colour from a variable, never a literal. Reskinning a product then means changing values.
The values are the theme’s settings, with the defaults it ships. Your product changes them in its [tool.splent.config] (see Feature settings). One an administrator should change from a panel is a runtime parameter, read with param().
Filling the public shell’s slots
{% for hook in get_template_hooks("layout.head") %}{{ hook() | safe }}{% endfor %}
A feature adds markup to the shell through its slots, without editing it, as in the product’s layout (see Template hooks). Which slots a public shell opens is the theme’s choice, and the theme documents them. Name them like the product layout’s (layout.head, layout.nav, layout.footer, layout.scripts) and a feature fills the same slot in either shell.
Keeping a fallback for an empty slot
{% set hero = render_template_hooks("home.hero") %}
{% if hero %}{{ hero }}{% else %}<section class="hero">{{ _('Welcome') }}</section>{% endif %}
render_template_hooks(name) returns the slot’s joined output, empty when no hook drew anything, so the page keeps its own markup. A hook meant for one page checks request.endpoint and returns "" elsewhere.
Adding a skin
from splent_framework.blueprints.base_blueprint import create_blueprint
skin_ocean_bp = create_blueprint(__name__)
OCEAN_TOKENS = {"primary": "#1E63BD", "accent": "#0E9F8E", "bg": "#F5FAFC"}
def init_feature(app):
from splent_framework.assets.asset_registry import register_asset
app.config["MY_THEME_TOKENS"] = OCEAN_TOKENS
register_asset(
"css", "skin_ocean.assets", order=200, subfolder="css", filename="skin_ocean.css"
)
A skin is a light feature with no models and no routes of its own. It gives the theme its tokens and registers a stylesheet from its assets/css/ at order 200, after every other style.
A skin requires the theme, so it loads after it and its tokens are the ones the theme reads. Install or remove a skin and the whole public look changes, with no edit to the theme or your templates.
Adding an entry to the main nav
from splent_framework.nav.nav_registry import register_nav_item
def init_feature(app):
register_nav_item("events", "Events", "/events", order=20)
Each content feature declares its public nav entry, so the menu follows the product’s features. The theme reads get_nav_items(), sorted by order, and draws them.
children turns an entry into a dropdown. It takes a list of {"label", "href"} dicts, or a function that returns one on every render. If the function fails, the entry is a plain link.
The signed-in application’s menus come from another registry, the menu registry.
Reference
register_nav_item
| Argument | Default | Meaning |
|---|---|---|
key |
Required | Stable identifier. Registering it again replaces the entry |
label |
Required | The text, translated when the theme draws it |
href |
Required | The URL the entry opens |
order |
100 |
Lower first, ties by label |
icon |
None |
An icon name |
children |
None |
A list of {"label", "href"}, or a function that returns one |
See also
- Menus and shells, for
shell(), the menu registry and the assets of each shell. - Template system, for template hooks and the product layout.
- Feature settings, for where a theme’s values come from.