Smarty Template Engine
Less than 1 minute
Smarty Template Engine
Smarty is a template engine for PHP that facilitates the separation of presentation from application logic. Hazaar MVC supports Smarty (.tpl) as a first-class citizen.
Basic Syntax
Smarty uses curly braces {} for logic and variables.
<h1>Hello, {$name}!</h1>
{if $user.is_logged_in}
<p>Welcome back, {$user.name}!</p>
{else}
<a href="/login">Log In</a>
{/if}
<ul>
{foreach $items as $item}
<li>{$item}</li>
{/foreach}
</ul>View Helpers
Hazaar MVC View Helpers are automatically enabled in Smarty templates. If you initialize a helper in your controller:
// In Controller
$this->view->addHelper('Form');You can use it directly in your Smarty template as an object:
{$form->open()}
{$form->input('username')}
{$form->close()}Custom Functions
You can register custom PHP functions in your controller to be used within your Smarty views.
1. Register in Controller:
$this->view->registerFunction('myFunc', function($param) {
return "Processed: " . strtoupper($param);
});2. Call in Smarty:
{myFunc param="test value"}Layouts
When creating a layout file in Smarty, use the special {layout} function to render the inner view content.
<!DOCTYPE html>
<html>
<head>
<title>{$title}</title>
</head>
<body>
<div class="container">
<!-- Render the main view here -->
{layout}
</div>
</body>
</html>