Stencil

Advanced template engine for Java

View project onGitHub

Layouts

Stencil's built in block passing syntax makes layouts simple. You can build layouts with macros or, since templates themselves are basically macros, with the include output.

Macro Layout

To build a macro layout you simply define a macro

layout.st
	$macro myLayout(title) [header,content,footer] {
		<html>
		<head>
			<title>$title;</title>
		</head>
		<body>
			<header>
				$header;
			</header>
			<div class="content">
				$content;
			</div>
			<footer>
				$footer;
			</footer>
		</body>
	};
	
Then call that macro using block passing syntax from the same or different file.

index.st
	$$import 'layout.st';
	
	$myLayout(title='Welcome')
		header {
		}
		content {
		}
		footer {
		};
	

Include Layout

To build an include layout it is almost exactly the same as a macro layout except you declare a macro signature for the template itself using the '$$' header syntax.

layout.st
	$$(title) [header,content,footer];
	
	<html>
	<head>
		<title>$title;</title>
	</head>
	<body>
		<header>
			$header;
		</header>
		<div class="content">
			$content;
		</div>
		<footer>
			$footer;
		</footer>
	</body>
	
To use the layout you simply include the layout template while passing it parameters and blocks as usual.

index.st
	$include 'layout.st' (title='Welcome')
		header {
		}
		content {
		}
		footer {
		};