wirePopulateStringTags() function

Given a string ($str) and values ($vars), replace “{tags}” in the string with the values

  • The $vars should be an associative array of [ 'tag' => 'value' ].
  • The $vars may also be an object, in which case values will be pulled as properties of the object.

By default, tags are specified in the format: {first_name} where first_name is the name of the variable to pull from $vars, { is the opening tag character, and } is the closing tag char.

The tag parser can also handle subfields and OR tags, if $vars is an object that supports that. For instance {products.title} is a subfield, and {first_name|title|name} is an OR tag.

Example

$vars = [ 'foo' => 'FOO!', 'bar' => 'BAR!' ];
$str = 'This is a test: {foo}, and this is another test: {bar}';
echo wirePopulateStringTags($str, $vars);
// outputs: This is a test: FOO!, and this is another test: BAR!

Usage

// basic usage
$string = wirePopulateStringTags(string $str, $vars);

// usage with all arguments
$string = wirePopulateStringTags(string $str, $vars, array $options = array);

Arguments

NameType(s)Description
strstring

The string to operate on (where the {tags} might be found)

varsWireData, object, array

Object or associative array to pull replacement values from.

options (optional)array

Array of optional changes to default behavior, including:

  • tagOpen (string): The required opening tag character(s), default is '{'
  • tagClose (string): The optional closing tag character(s), default is '}'
  • recursive (bool): If replacement value contains tags, populate those too? (default=false)
  • removeNullTags (bool): If a tag resolves to a NULL, remove it? If false, tag will remain. (default=true)
  • entityEncode (bool): Entity encode the values pulled from $vars? (default=false)
  • entityDecode (bool): Entity decode the values pulled from $vars? (default=false)

Return value

string

String with tags populated.


Functions methods and properties

API reference based on ProcessWire core version 3.0.236

Latest news

  • ProcessWire Weekly #520
    In the 520th issue of ProcessWire Weekly we'll check out some of the latest additions to the ProcessWire module's directory, share some highlights from the latest weekly update from Ryan, and more. Read on!
    Weekly.pw / 27 April 2024
  • ProFields Table Field with Actions support
    This week we have some updates for the ProFields table field (FieldtypeTable). These updates are primarily focused on adding new tools for the editor to facilitate input and management of content in a table field.
    Blog / 12 April 2024
  • Subscribe to weekly ProcessWire news

“Indeed, if ProcessWire can be considered as a CMS in its own right, it also offers all the advantages of a CMF (Content Management Framework). Unlike other solutions, the programmer is not forced to follow the proposed model and can integrate his/her ways of doing things.” —Guy Verville, Spiria Digital Inc.