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]]` &options=`<li>[[+title]]</li>`]]

Parameters

ParameterDescription
inputJSON string or an array. Empty input is returned unchanged.
optionsThe template applied to each element.

Placeholders

Both spellings work inside options, 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"]` &options=`<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]]` &options=`<li><a href="[[+url]]">[[+title]]</a></li>`]]
</ul>

© PageBlocks 2019-present