Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Div
classapi-page

fluid.stringTemplate(template, values)

Section
Column
width70%

Simple string template system. Takes a template string containing tokens in the form of "%value". Returns a new string with the tokens replaced by the specified values. Keys and values can be of any data type that can be coerced into a string. Arrays will work here as well.

Code Block
javascript
javascript
bgColorwhite
borderStylenone
fluid.stringTemplate(template, values)

File name: Fluid.js

Parameters

Span
classborderless-table

template

(String) a string (can be HTML) that contains tokens embedded into it

values

(Object) a collection of token keys and values

Return Value

Span
classborderless-table

The string with the values inserted.

Column
width5%

Column

See Also


Examples

In the following example, a data object of key/value pairs is used to produce the string "Paused at: 12 of 14 files (100 Kb of 12000Gb)"

Code Block
javascript
javascript
var template = "Paused at: %atFile of %totalFiles files (%atSize of %totalSize)";
var data = {
    atFile: 12,
    totalFiles: 14,
    atSize: "100 Kb",
    totalSize: "12000 Gb"
};
var result = fluid.stringTemplate(template, data);

In the following example, an array of simple values is used to produce the string "Paused at: 12 of 14 files (100 Kb of 12000Gb)"

Code Block
javascript
javascript
var template = "Paused at: %0 of %1 files (%2 of %3)";

var atFile = "12";
var totalFiles = "14";
var atSize = "100 Kb";
var totalSize = "12000 Gb";
var data = [atFile, totalFiles, atSize, totalSize];

var result = fluid.stringTemplate(template, data);

In the following example, an array of mixed, complex values is used to produce the string "Paused at: 12 of 14 files (100 Kb of 12000Gb)"

Code Block
javascript
javascript
var template = "Paused at: %0 of %1 files (%2 of %3)";

var atFile = "12";
var totalFiles = "14";
var atSize = { // This represents a complex object type that has a toString method.
    toString: function () {
        return "100 Kb";
    }
};
var totalSize = "12000 Gb";
var data = [atFile, totalFiles, atSize, totalSize];

var result = fluid.stringTemplate(template, data);