PHTML Template Engine
Less than 1 minute
PHTML Template Engine
PHTML (.phtml) files are standard PHP scripts used as templates. This approach gives you the full power of PHP within your view layer.
Basic Syntax
In a PHTML file, $this refers to the internal Hazaar\View object (or Hazaar\View\Layout).
Accessing Data
Any data assigned in the controller is available as public properties of $this.
Controller:
$this->view->title = 'Home Page';
$this->view->items = ['One', 'Two'];View (index.phtml):
<h1><?php echo $this->title; ?></h1>
<ul>
<?php foreach ($this->items as $item): ?>
<li><?php echo $item; ?></li>
<?php endforeach; ?>
</ul>Accessing Helpers
View Helpers are also accessed as properties of $this once added.
<?php echo $this->form->open(); ?>
<!-- Form HTML -->
<?php echo $this->form->close(); ?>Custom Functions
You can call custom view functions using $this->functionName().
1. Register in Controller:
$this->view->registerFunction('formatCurrency', function($value) {
return '$' . number_format($value, 2);
});2. Call in PHTML:
<span>Price: <?php echo $this->formatCurrency(19.99); ?></span>Layouts
When creating a layout in PHTML, use the $this->layout() method to output the inner view content.
<!DOCTYPE html>
<html>
<head>
<title>My Application</title>
</head>
<body>
<div class="content">
<!-- Render the main view here -->
<?php echo $this->layout(); ?>
</div>
</body>
</html>