Templates that read like the page they render.¶
Looma brings Laravel Blade's syntax to Spring Boot. Write HTML with {{ }} and @directives, and get layouts, components, validation errors and CSRF without attributes bolted onto your markup. Drop-in for a Spring MVC controller, with a sandboxed expression language and a startup check that refuses to boot on a broken template.
@extends('layouts.app')
@section('content')
<h1>Hello, {{ $user.name }}!</h1>
@forelse($posts as $post)
<li @class({'first': $loop->first})>
{{ $post.title }}
</li>
@empty
<li>No posts yet</li>
@endforelse
<form method="post" action="/posts">
@csrf
<input @field('title')>
@error('title')<small>{{ $message }}</small>@enderror
</form>
@endsection
<html layout:decorate="~{layouts/app}">
<div layout:fragment="content">
<h1 th:text="|Hello, ${user.name}!|"></h1>
<th:block th:if="${!posts.isEmpty()}">
<li th:each="post, s : ${posts}"
th:classappend="${s.first} ? 'first'"
th:text="${post.title}"></li>
</th:block>
<li th:if="${posts.isEmpty()}">No posts yet</li>
<form method="post" th:action="@{/posts}"
th:object="${form}">
<input th:field="*{title}">
<small th:if="${#fields.hasErrors('title')}"
th:errors="*{title}"></small>
</form>
</div>
T(), constructors, reflection or bean access unless you opt in.How a page is rendered¶
Four steps, from the template you write to the HTML your visitor gets. Click a step, or let it play.
<h1>Hello, {{ $user.name }}!</h1>
<ul>
@foreach($posts as $post)
<li>{{ $post.title }}</li>
@endforeach
</ul>
{{ }} prints a value, escaped for HTML. Directives such as @foreach and @if control the page. The HTML around them stays exactly as you wrote it.<h1>Hello, {{ $user.name }}!</h1>
<ul>
@foreach($posts as $post)
<li>{{ $post.title }}</li>
@endforeach
</ul>
@GetMapping("/")
String home(Model model) {
model.addAttribute("user", new User("Ada"));
model.addAttribute("posts", List.of(
new Post("Hello Looma"),
new Post("Layouts and components")));
return "home"; // templates/home.looma.html
}
$user and $posts in the template. Records, getters and maps all work, and so do the request, the signed-in user and validation errors.<h1>Hello, Ada!</h1>
<ul>
<li>Hello Looma</li>
<li>Layouts and components</li>
</ul>
Install¶
One dependency. Templates go in src/main/resources/templates/ as *.looma.html and controllers return the view name as usual.
What you get¶
-
Layouts, components, slots and stacks
@extends/@sectioninheritance,<x-alert>components with@propsand named slots,@pushinto the layout's<head>,@oncefor a component's styles. No layout dialect to add. -
Forms that know Spring
@bindand@fieldfillname,idandvaluefrom the model object, keep the rejected input after a failed@Validsubmit, and@errorshows the message from yourMessageSource. -
Safe by default
Escaped output, a sandboxed SpEL subset, bounded loops and recursion, buffered output so an error never sends half a page, and
@auth/@canthat fail closed. -
Built to be extended
Every directive is one small class behind a public API. Yours are plain Spring beans; so are template helpers and the beans you expose with
@TemplateAccessible.
Start here¶
- Install it: one dependency, no other setup.
- Getting started: a page, a layout, a validated form and a component in ten minutes.
- Migrating from Thymeleaf: the
th:*to Looma mapping, and how to run both engines while you move.
Looma is open source under the Apache License 2.0 and lives on GitHub.