n:attributes vs {tags}
-
Rule:
n:attributesSHOULD be preferred over{tags}when working with HTML elements. -
Reason: n:attributes keep the template cleaner and more readable by integrating Latte logic directly into HTML attributes.
Example (Correct):
<ul n:if="$items">
<li n:foreach="$items as $item">{$item->name}</li>
</ul>
<a n:href="Product:detail $product->id" n:class="$isActive ? active">
{$product->name}
</a>
Example (Less preferred):
{if $items}
<ul>
{foreach $items as $item}
<li>{$item->name}</li>
{/foreach}
</ul>
{/if}
When to Use {tags}
-
Rule:
{tags}SHOULD be used for:- Block definitions (
{block},{define}) - Layout inheritance (
{layout},{extends}) - Including files (
{include}) - Variable assignments (
{var},{default}) - Complex logic that doesn't fit into n:attributes
- Block definitions (
Example:
{layout '@layout.latte'}
{block content}
{include 'partials/header.latte'}
{var $total = count($items)}
<p>Total: {$total}</p>
{/block}
Summary:
n:attributesSHOULD be preferred for conditions and loops on HTML elements.{tags}SHOULD be used for blocks, layouts, includes, and variable assignments.