PHTML Template Engine
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>$this->get('title') and $this['title'] return the same value, and $this->getData() returns all of it. This includes data passed to $this->view('page', [...]) when the view is rendered inside a layout.
Helper names
A property name checks loaded helpers first, then view data, then tries to load a helper with that name. Don't give view data the same name as a helper you use (e.g. html), or one will hide the other.
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>