Snippets
The snippet
statement allows for defining reusable code snippets and is therefore also the basis for any kind of recursion in Automad. A snippet always inherits the context of the calling code. To use a snippet, it is enough to put the snippet's name in statement delimiters.
<# Define snippet with the name "example". #>
<@ snippet example @>
<# Some code ... #>
<@ end @>
<# Use snippet somewhere later. #>
<@ example @>
Snippets have to be defined before being used.
Recursion
A simple example for a recursive usage of snippets is a site tree. Please also take a look at the menu snippet of the Skeleton theme.
<div class="menu">
<@ snippet navigation @>
<@ if @{ :pagelistCount } @>
<ul class="menu-list">
<@ foreach in pagelist @>
<li>
<a
href="@{ url }"
<@ if @{ :current } ~@>
class="has-background-info has-text-white"
<@~ end @>
>
@{ title }
</a>
<@ if @{ :currentPath } @>
<@ navigation @>
<@ end @>
</li>
<@ end @>
</ul>
<@ end @>
<@ end @>
<@ newPagelist {
type: 'children',
excludeHidden: false
} @>
<@ with '/' @>
<@ navigation @>
<@ end @>
</div>
Inheritance
The fact the snippets in a template file can be overridden after including it is part of the basic principle of inheritance in Automad. Read more about extending templates and reusing their code in the inheritance guide.