Notch Templates
Built-in Commands
The 14 commands wired by BasicNotchTemplateCommands
Calling BasicNotchTemplateCommands.addTo(templates) on a NotchTemplates adds these commands. The list is in BasicNotchTemplateCommands.COMMANDS: helper, content, layout, for, if, include, macro, expand, import, fragment, set, comment, require and raw. else and end are parts of if and for. elseif is part of if. They are not separate commands.
Each command is on its own line, written #name with no space after the # (see Template Syntax).
Control flow
#if <expr>/#elseif <expr>/#else/#end- conditional blocks. The contents between#if(or a passing#elseifbranch) and the next branch or#endrender only when the condition is true.
#if user.admin
Admin panel
#elseif user.name == "guest"
Please log in
#else
Welcome, ${ user.name }
#end
Only the boolean true passes. Any other value fails, including a non-empty string, a non-empty list and a number. To test for a value, compare it: #if name != null.
#for <name> in <expr>/#else/#end- iterate over an iterable. The current item is bound to<name>inside the block. The loop also definesindex, the 0-based position of the current item. The optional#elsebranch renders when the iterable is empty ornull.
<ul>
#for pet in pets
<li>${ index }: ${ pet }</li>
#else
<li>No pets yet</li>
#end
</ul>
With pets set to List.of("cat", "dog") this renders <li>0: cat</li> and <li>1: dog</li>. With an empty list it renders <li>No pets yet</li>.
The Notch language form for x in xs index i is not available in templates. Use index.
Layout and composition
-
#layout "<path>"- render this template inside another (layout) template. Put it on the first line. The rest of this template is the body. The body fills the layout's unnamed#contentslot. -
#content- has three forms:#contentin a layout marks the slot for the body.#content <name>in a layout marks a named slot. It is empty if the child does not fill it.#content <name> with…#endin a layout marks a named slot with default content. In a child template it fills that slot. In a child template the name andwithare required, and the layout must have a slot with that name.
<head>
#content title with
<title>My App</title>
#end
</head>
<body>
#content
</body>#layout "base.html"
#content title with
<title>Home | MyApp</title>
#end
<main>...</main>Rendering index.html gives the layout with <title>Home | MyApp</title> in the head and <main>...</main> in the body.
#include "<path>"- render another template at this point. The included template sees the variables of the current template.
#include "nav.html"
#fragment <name>/#end- define a named section of a template. In a full render the fragment renders in place.renderTemplate("<path>#<name>", vars)renders only that fragment. Another template can render it with#importand#expand.
<h1>Contacts</h1>
#fragment rows
#for c in contacts
<tr><td>${ c }</td></tr>
#end
#end
renderTemplate("contacts.html#rows", vars) renders only the <tr> rows.
#import "<path>"- load the macros and fragments of another template into the current template without rendering it.#import "<path>" as <alias>puts them under the alias, for example#expand cards.card(...).#import <qualified.ClassName>- bring a JVM type into scope by its simple name, for example#import java.time.LocalDateand then${ LocalDate.now() }.
Variables and reuse
#set <name> = <expr>- bind<name>to the result of<expr>. If<name>already exists,#setupdates it, also from inside a#forloop.
#set total = 0
#for n in [1, 2, 3]
#set total = total + n
#end
Total: ${ total }
renders Total: 6.
#macro <name>(<params>)/#end- define a reusable template snippet that takes parameters. A macro does not render where it is defined. A parameter can declare a type, as inname: String. The argument is coerced to that type.#expand <name>(<args>)- render a macro (or a fragment) with arguments. Macros are not functions:${ card(1) }fails. Use#expand.
#macro card(title, body)
<div class="card"><h2>${ title }</h2>${ body }</div>
#end#import "cards.html"
#expand card("Hello", "First card")
renders <div class="card"><h2>Hello</h2>First card</div>.
#helper <qualified.ClassName>- create an instance of a Java class and make its members available as names in the template. Use the binary name for a nested class, for example#helper com.example.Main$ViewHelper. For a plain class, its public fields are available. If the class implementsNotchTemplateHelper, its properties and methods are available, and so are the default methodsraw(value)andoptionsForEnum(...). A template has one helper. If a template has more than one#helper, the last one is used.
public class ViewHelper implements NotchTemplateHelper {
public String shout(String s) { return s.toUpperCase() + "!"; }
}
#helper com.example.ViewHelper
${ raw("<b>bold</b>") } ${ shout("hi") }
renders <b>bold</b> HI!.
Utilities
#require <name>: <Type>- check that a binding is present and has the given type before rendering. Add!after the type to also rejectnull. Separate several requirements with commas. If a check fails, rendering fails with a clear message, for examplemissing required symbol "name".
#require name: String!, age: Integer
${ name } is ${ age }
In #require and typed macro parameters, a short type name such as String or List is looked up in java.lang, java.io and java.util. Other types need their fully-qualified name.
-
#comment/#end- everything between is ignored. Expressions and commands inside are not evaluated. Comments do not appear in output. The first#endends the comment, so do not put a block command such as#if…#endinside a comment. -
#raw/#endraw- everything between is written to the output as literal text. Expressions and commands inside are not evaluated. The block ends with#endraw, not#end.
#raw
${ name } and #for stay as text
#endraw
renders ${ name } and #for stay as text.
See also
- Custom Commands to add your own commands alongside these 14.