This documentation is currently being moved to our new documentation site.

Please view or edit the documentation there, instead.

If you're looking for Fluid Project coordination, design, communication, etc, try the Fluid Project Wiki.

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

fluid.stringTemplate(template, values)

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.

fluid.stringTemplate(template, values)

File name: Fluid.js

Parameters

template (String) a string (can be HTML) that contains tokens embedded into it
values (Object) a collection of token keys and values

Return Value

The string with the values inserted.

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)"

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)"

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)"

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);
  • No labels