Skip to main content

How to pass data into a template for display (v2)

A PHP object called $viewModel is used to store data for presentation in each template. The viewModel for a given page provides methods to set or retrieve required data. You can assign additional data directly to this object if you wish, by setting properties on the viewModel from the controller, which holds a reference to the viewModel, or from within the viewModel itself:

// Example: Set the 'pageTitle' property on a viewModel from the controller for that page:
$this->viewModel->someProperty = 'Some name';

Within the template, you can then retrieve your data from the viewModel directly:

<h1><?php echo $viewModel->someProperty; ?></h1>

If the data comes from an external or untrusted source, then you should also escape it for display by passing it through the xss() function, which is available in all templates:

<h1><?php echo xss($viewModel->someProperty); ?></h1>

Copyright, all rights reserved.