Skip to content

Kirby 6.0.0

$field->replace()

It parses any queries found in the field value.

$field->replace(array $data = [], string|null $fallback = ''): Field

Parameters

Name Type Default Description
$data array []
$fallback stringornull '' Fallback for tokens in the template that cannot be replaced (null to keep the original token)

Return type

Kirby\Content\Field

This method does not modify the existing $field object but returns a new object with the changes applied. Learn more →

All placeholders in the field value run as queries with full access to Kirby's PHP API, not only the ones you pass with $data. Only use this method with fields that you would trust to contain PHP code. Never use it with content that comes from site visitors or with fields that Panel users with limited permissions can edit.

Learn more about trusted queries ›

Examples

Given the field contains string templates, use this method to replace them with values:

Example text:

Text: The company behind {{ product }} and the responsible data controller for this website is the {{ company }}.
<?= $page->text()->replace(
    [
        'company' => 'Content Folder GmbH & Co. KG',
        'product' => 'Kirby CMS'
    ]
);

Will result in

The company behind Kirby CMS and the responsible data controller for this website is the Content Folder GmbH & Co. KG.