Repository navigation
Views and templates
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 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.
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>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.
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)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 itUse false while building the interface and true for an installed application.
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 |
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.