Skip to content

pbJson ​

Renders a JSON value through an inline template. Useful for a field that stores JSON — a repeater, a key-value list, a gallery — when you want markup without a loop in the template.

[[pbJson? &input=`[[+items]]` &tpl=`<li>[[+title]]</li>`]]

Parameters ​

ParameterDescription
inputJSON string or an array. Empty input is returned unchanged.
tplThe template applied to each element. &options is the deprecated spelling of the same thing — it still works for one major version and writes a line into the MODX log.
outputSeparatorGlue between rendered elements, \n by default.
toPlaceholderPut the output into a MODX placeholder and return nothing.

Placeholders ​

Both spellings work inside tpl, so the same template can be used from MODX and from Fenom:

[[+title]]    {$title}

A list of objects renders once per element, with the element's keys as placeholders. A single object renders once. A flat list of scalars exposes each value as item:

[[pbJson? &input=`["red","green"]` &tpl=`<span>[[+item]]</span>`]]

Nested values are skipped rather than printed as Array.

Also a modifier ​

The same code backs the |pbJson Fenom modifier:

php
{$block->items | pbJson : '<li>{$title}</li>'}

The modifier needs the snippet

|pbJson calls this snippet by name. If the package was never installed on the site — files copied by hand, say — the modifier silently returns nothing. This was a real bug in early 3.0 builds, where no snippets were registered at all.

Example ​

A repeater field items with title and url in each row:

<ul>
    [[pbJson? &input=`[[+items]]` &tpl=`<li><a href="[[+url]]">[[+title]]</a></li>`]]
</ul>

© PageBlocks 2019-present