Skip to content

Views and templates

yassinezerguine08 edited this page Oct 23, 2026 · 1 revision

Views and templates

A view names its template

A view is an object that implements View:

public interface View {
	/** The template, named the way Thymeleaf names one: {@code page} or {@code page :: block}. */
	String template();
}

The name maps to a file under templates/ on the classpath, with .html added. Oriel's own test views show both kinds:

template() returns Oriel renders
"oriel-test/page" the whole of templates/oriel-test/page.html
"oriel-test/testing :: status" only the block of templates/oriel-test/testing.html marked th:fragment="status"

A whole page is what the window opens on. A block is what an action answers with, for htmx to swap into the page.

The template reads the view as view

The view object itself is the template's view variable, so a record's accessors are read directly. From Oriel's tests:

public record Status(String holder) implements View {
	@Override
	public String template() {
		return "oriel-test/testing :: status";
	}
}
<div th:fragment="status" id="ledger-status">
	<p>Ledger is unlocked by <span th:text="${view.holder}">nobody</span></p>
</div>

The text inside an element, such as nobody, is only there for opening the file directly. Thymeleaf replaces it.

Putting a page together from blocks

A page view usually holds the views of its blocks. th:with hands each block its own view, so the same block renders the same way whether it comes with the whole page or alone from an action. From Getting started:

public record IndexView(GreetingView greeting) implements View {
	@Override
	public String template() {
		return "index";
	}
}
<th:block th:with="view=${view.greeting()}">
	<p th:fragment="greeting" id="greeting" th:text="'Hello, ' + ${view.name()} + '!'">Hello!</p>
</th:block>

Swapping a block into the page

The block's root element carries an id, and the form or button that asks for it targets that id and replaces it whole:

<form hx-post="/action/unlock-ledger" hx-target="#ledger-status" hx-swap="outerHTML">…</form>

The action returns the block's view, here a Status, so what comes back is again a <div id="ledger-status">, and the page can be swapped as often as needed.

The index view

Every address that is not a file, a window command or an action renders the index view given to Routes.create(…). Given as a supplier, it is asked for again on every request, so it can change while the application runs:

Routes.create(application.id(), templates, token, window.control(), actions, greeter::start)

Caching and development

new Templates(false)   // read every template afresh on each request: an edited template shows on the next one
new Templates(true)    // parse each template once and keep it

Use false while building the interface and true for an installed application.

Static files

Everything under static/ on the classpath is served under /static/, such as static/css/app.css at /static/css/app.css. Oriel's own scripts are served under /oriel/:

Script What it does
/oriel/htmx.min.js htmx 2.0.10, shipped with Oriel
/oriel/window.js The title bar, resizing, file dialogs and the theme toggle; see The window
/oriel/failure.js The notice for a failed action; see Actions

Expressions

Keep template expressions to reading the view: accessor calls, joins, ? :, and Thymeleaf's #strings and #numbers helpers. Thymeleaf restricts reaching Java classes from a template, such as T(…) or new, and the view is the place for anything more.

Clone this wiki locally