Quick start
Render a template with data through the facade:
use Boshnik\PageBlocks\Facades\View;
View::make('@INLINE <p>{$title}</p>', ['title' => 'Home']);or through the helper — the same thing, shorter:
view('file:templates/base.tpl', ['title' => 'Home']);Both take the same signature:
view(string $template, array $params = [], callable $callback = null): stringThe third argument is a handler for the finished HTML. What the prefix in the template name means is covered in Template types.
💡 From a controller
Route::get('/', function () {
return view('file:templates/base', ['name' => 'PageBlocks']);
// => looks for core/App/elements/templates/base.tpl
});🔧 From a MODX snippet
A MyProfile snippet rendering the current user's profile:
<?php
$user = $modx->user;
return view('file:blocks/profile.tpl', [
'username' => $user->get('username'),
'email' => $user->getOne('Profile')->get('email'),
]);core/App/elements/blocks/profile.tpl:
<div class="profile">
<h2>{$username}</h2>
<p>Email: {$email}</p>
</div>You do not need to pass $modx
View puts it into every template itself — see Available data. The example above could have used {$modx->user->username} and no parameters at all.
Empty instead of an error
When a template is not found, view() returns an empty string and the reason goes to the MODX log naming the source it looked in: [Fenom] chunk 'header' not found in the database; for a file write file:header, or [Fenom] file blocks/card.tpl not found under …. A blank spot on the page is worth looking for there, not in your {if}.