WireTextTools::populatePlaceholders() method

Given a string ($str) and values ($vars), populate placeholder “{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.

Available since version 3.0.126. Use wirePopulateStringTags() function for older versions

Example

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

Usage

// basic usage
$string = $wireTextTools->populatePlaceholders(string $str, $vars);

// usage with all arguments
$string = $wireTextTools->populatePlaceholders(string $str, $vars, array $options = []);

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 (i.e. field not present), remove it? (default=true)
  • removeEmptyTags (bool): If a tag value resolves to blank string, false or NULL, remove it? (default=true) 3.0.237+
  • entityEncode (bool): Entity encode the values pulled from $vars? (default=false)
  • entityDecode (bool): Entity decode the values pulled from $vars? (default=false)
  • allowMarkup (bool): Allow markup to appear in populated variables? (default=true)

Return value

string

String with tags populated.


WireTextTools methods and properties

API reference based on ProcessWire core version 3.0.248

Latest news

  • ProcessWire Weekly #582
    The 582nd issue of ProcessWire Weekly brings in all the latest news from the ProcessWire community. Modules, sites, and more. Read on!
    Weekly.pw / 5 July 2025
  • New ProcessWire admin redesign
    When you upgrade to ProcessWire 3.0.248 or newer (currently the dev branch), you’ll immediately notice something new and beautiful…
    Blog / 9 May 2025
  • Subscribe to weekly ProcessWire news

“To Drupal, or to ProcessWire? The million dollar choice. We decided to make an early switch to PW. And in retrospect, ProcessWire was probably the best decision we made. Thanks are due to ProcessWire and the amazing system and set of modules that are in place.” —Unni Krishnan, Founder of PigtailPundits