Components and Templates

Verze:

05. 01. 2026

Zodpovědná osoba:

Dominik Šlechta

Templates MUST declare expected variables with {default} at the beginning. Components MUST be rendered using {control}. AJAX-updatable parts MUST use {snippet} tags.

Default Variables Declaration

  • Rule: Every component/template MUST declare expected variables with default values at the beginning using {default} or {var}.

  • Reason: This documents which variables the template expects, provides fallback values, and prevents undefined variable errors.

Example (Correct):

{**
 * Product card component
 * @param Product $product - Product entity
 * @param bool $showPrice - Whether to display price
 * @param string $class - Additional CSS classes
 *}
{default $product = null}
{default $showPrice = true}
{default $class = ''}

<div class="product-card {$class}">
	{if $product}
		<h3>{$product->name}</h3>
		<p n:if="$showPrice">{$product->price|number:2} Kč</p>
	{else}
		<p>Product not found</p>
	{/if}
</div>

Example (Incorrect):

<!-- No declaration of expected variables -->
<div class="product-card">
	<h3>{$product->name}</h3>
	<p>{$product->price}</p>
</div>

Component Rendering

  • Rule: Nette components MUST be rendered using {control componentName}.

Example:

{control productList}
{control cartWidget}
{control form}

Snippets for AJAX

  • Rule: AJAX-updatable parts MUST be wrapped in {snippet} tags.

Example:

{snippet cartCount}
	<span class="cart-count">{$cartItemCount}</span>
{/snippet}

Summary:

  • Templates MUST declare expected variables with {default} at the beginning.
  • Components MUST be rendered using {control}.
  • AJAX-updatable parts MUST use {snippet} tags.