Skip to content

Available data

Besides whatever you pass in, a template always has $modx and the previous request's flash data.

🧠 $modx

View::render() puts $modx into the data of every template — the same object you use in PHP:

fenom
{$modx->resource->id}                {* current page ID *}
{$modx->resource->pagetitle}
{$modx->resource->get('template')}   {* any field *}

{$modx->user->username}
{$modx->user->id}
{$modx->user->get('email')}
{$modx->user->getGravatar()}

It is a variable, not an accessor

Write {$modx}, not {$.modx} — there is no accessor by that name, and trying one is a compile error. The variable, on the other hand, is always there: render() puts it into every template's data before any prefix parsing, so nested chunk, render and include see it too.

💬 Flash messages

A controller puts them into the session before redirecting (->success(), ->error(), ->with(), ->withErrors(), ->withInput()), and View unpacks them into template variables:

VariableContents
old_inputthe values submitted last time
errorsone error per field — what goes under the input
success_messagethe success message
error_messagethe error message
fenom
{if $errors.email}
  <p class="error">{$errors.email}</p>
{/if}

<input type="text" name="email" value="{$old_input.email}">

{if $success_message}
  <div class="alert alert-success">{$success_message}</div>
{/if}

{if $error_message}
  <div class="alert alert-danger">{$error_message}</div>
{/if}

errors is always collapsed to one string per field even when the validator returned several: the first one is taken. That is for the markup's sake — only one message fits under a field anyway.

Flash data lives for one render

It is stored in $_SESSION['pageblocks']['flash'] and wiped by the next Response::send(). Reload the page and the message is gone.

🌍 Global data $.pb

The site file:

core/App/Helpers/fenom/data.php

returns an array that becomes available in every template through the pb accessor:

php
return [
    'package' => 'PageBlocks',
    'support_email' => 'support@example.com',
    'payment' => [1 => 'Bank transfer', 2 => 'Cash'],
];
fenom
{$.pb.package}          {* PageBlocks *}
{$.pb.support_email}
{$.pb.payment[$order.payment]}

The dot before pb is mandatory

$.pb is an accessor, not a variable. {$pb.package} is not an error — it is an ordinary undeclared variable: it renders as nothing, silently. That typo shows up neither in the log nor on the page, only as missing text.

The accessor is registered only if data.php exists. No file, no $.pb.

This is the place for reference data that does not change between requests: status labels, units, code → name maps. The file is read when View is initialised, so it must be cheap — no database queries in it.

© PageBlocks 2019-present