Loops {foreach}, {for}, {while}

foreach, for and while loops behave exactly the same as their PHP counterparts.

{foreach$resultas$row}
<span>{$row->title}</span>
{/foreach}

{while$row = $result->fetch()}
<span>{$row->title}</span>
{/while}

{for$i = 0; $i < 10; $i++}
<span>Item #{$i}</span>
{/for}

Inside the foreach loop the $iterator variable is initialized. It holds important information about
the current loop. Its API features iteration counter
and methods identifying odd, even, first and last iteration:

isFirst() – is this the first iteration?

isLast() – is this the last iteration?

getCounter() – iteration counter, starts from 1

isOdd() – is this iteration odd?

isEven() – is this iteration even?

Object behind the $iterator variable has Nette\Object class characteristics, that's why
you can write $iterator->first instead of $iterator->isFirst() and alike. For example:

{if $iterator->first}<table>{/if} can be replaced with {first}<table>{/first}.
Similarly, the condition with last can be also written as {last}</table>{/last}. Finally, the
equivalent of {if !$iterator->last} … {/if} notation (ie. not the last item) is {sep} … {/sep}
macro. Usually it marks the separator between items – comma, for example. This way the redundant right comma won't print:

{foreach$itemsas$item} {$item} {sep}, {/sep} {/foreach}

There are two special macros you can use inside the loop – {continueIf ?} and {breakIf ?} which
jump to the next iteration and end the loop, respectively, if the conditions are met:

{foreach$rowsas$row}
{continueIf$row->parent == NULL}
...
{/foreach}

Variable declaration {var} and {default}

We can declare variables directly in the template by using {var} macro:

Capturing to variables {capture}

If you want to capture the output only for a helper to be applied it is easier to
use the helper on the {block} instead:

{block|strip}
<ul><li>Hello World</li></ul>
{/block}

File including {include}

You can include other templates into the current template. The included file has access to the global variables of the current
template. Additionally, we can also pass other variables listed in the macro:

In addition to including files the {include} macro is also used to include blocks.

Code evaluation {? expression}

This macro evaluates the code written inside but it does't print it.

Header {contentType}

Switches Context-Aware Escaping to the context determined by the
parameter so that when printing, the variables are escaped using the rules of the context. For example
{contentType xml} would switch to the XML escaping mode, meanwhile {contentType text} disables escaping
completely.

If the parameter is a complete MIME type the correct HTTP header is sent to the browser:

Latte mode can easily be used in JavaScript, you just have to avoid the notation above by writing a space after the opening
brace { var: 123} or by using quotes around the identifier {'var': 123}.

n:class

In a typical situation we could be trying to print an unknown amount of entries from the database as <li>
elements and mark the last entry and every odd item with corresponding CSS classes for further styling. In case the last line is
odd as well, we will require both classes, but we want to avoid empty classes like <li class=" ">.

Variable dumping {dump}

Break point {debugbreak}

Specifies the place where code execution will break. It is used for debugging purposes for the programmer to inspect the
runtime environment and to ensure the code runs as expected. It supports Xdebug and PhpEd. Additionally, you can specify a condition when the code
should break.

{debugbreak} {* breaks the program *}
{debugbreak $counter == 1} {* breaks the program if the condition is met *}