Skip to content

Quick start

Render a template with data through the facade:

php
use Boshnik\PageBlocks\Facades\View;

View::make('@INLINE <p>{$title}</p>', ['title' => 'Home']);

or through the helper — the same thing, shorter:

php
view('file:templates/base.tpl', ['title' => 'Home']);

Both take the same signature:

php
view(string $template, array $params = [], callable $callback = null): string

The 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

php
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
<?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:

fenom
<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 as [Fenom]: Template … not found. A blank spot on the page is worth looking for there, not in your {if}.

© PageBlocks 2019-present