Stacey
Templating Language
Stacey pre-parses each template as plain text before it is rendered out as html. As the templating system runs off regular expressions, there is no need to explicitly wrap the language constructs in <?php ?> tags.
Language Constructs
Within stacey templates, you have access to three language constructs: get blocks, foreach loops & boolean if statements.
- get
-
get "/url" do # do stuff end
- foreach
-
foreach $collection do # do stuff endforeach
- if
-
if @variable do # do stuff endif
- if not
-
if !@variable do # do stuff endif
Variable Context
Once you are inside a foreach loop, the variable context changes to the current object being referenced by the loop.
If you want to temporarily shift to the context of a specific page, you can add a get block.
get "/projects/project-1" do <p>@page_name</p> end
This will change the context from the current page being viewed to the /projects/project-1 page. This means $children will be filled with the children of the /projects/project-1 page, @page_name will equal 'Projects', etc.
So, if you wanted to only list children of the /projects folder which do not contain children of their own, you could construct the following partial:
get "/projects" do
foreach $children do
if !$children do
<p><a href="@url">@page_name</a></p>
endif
endforeach
end
Limiting foreach Loops
It is possible to limit foreach loops using a $collection[start:limit] syntax.
The array will essentially be sliced from the provided start until the limit.
The start & limit are both optional. You could specify $collection[:2] to only list the first two items, or $collection[2:] to list all items except the first two.
The following partial would list the third & fourth items from the $children collection:
<ol id="navigation">
foreach $children[2:2] do
<li><a href="@url">@page_name</a></li>
endforeach
</ol>
Nesting foreach Loops
To give you the ability to pass through multiple levels of objects, foreach loops can also be nested within themselves (although using recursive partials would be a more elegant solution).
<ol id="navigation">
foreach $root do
<li><a href="@url">@page_name</a>
if $children do
<ol>
foreach $children do
<li><a href="@url">@page_name</a>
endforeach
</ol>
endif
</li>
endforeach
</ol>