sub()

Substitute values into strings where the corresponding placeholders have been entered.

This function is valid in v2.0.0 to v2.24.3. This function has been downloaded 78 times.

Parameters

{string} template
The string containing placeholders to be filled in and returned. A placeholder must correspond to a value in opt_subs and its name must be surrounded in curly braces (eg. "Hello {name}!" contains the name placeholder). If a placeholder refers to a number, a ternary operator can be used (eg. "You have {apples} apple{apples?s:}"). What appears between the ? and the : will replace the placeholder if the variable before the ? is not 1. What appears after the : will replace the placeholder if the variable before the ? is 1. A 4-ary (AKA quaterary) operator can also be used if a placeholder refers to a number (eg. "You have {apples?{apples}:one:no} apple{apples?s:}"). When using a 4-ary operator, whatever appears after the second : will replace the placeholder if the variable before the ? is 0. If a placeholder is not ternary or 4-ary but ends with #, opt_funcs will be called on the value and the return value will be used. If a placeholder is not ternary or 4-ary but ends with # followed by a name (eg. #ordinalize), the function with that property name under opt_funcs will be called for the value and the return value will replace the placeholder. If a placeholder evaluates to a function the function will be called all of the ternary or 4-ary values as arguments. Nested expressions are supported.
{Array|Object=} opt_subs Optional, Default:global
Array or object from which to pull the values to be inserted into template.
{Array.|Object.|Function=} opt_funcs Optional, Default:YourJS
If this is a function it can be used to modify the values filled in within template. If this is an object or an array its properties or array items can be referenced to modify the values filled in within template.

Returns

{string}
Returns template with all of the valid placeholders filled in with their substitutions as found in opt_subs.

Required Function

This function directly requires the following function which is included automatically:

  • has()
    Every object descended from Object inherits the hasOwnProperty method. This method can be used to determine whether an object has the specified property as a direct property of that object; unlike the in operator, this method does not check down the object's prototype chain.