Base String Template Module
This module provides the basis for String templates using one or several functions as filters to modify the input. See lb.base.template.applyFilters(input...,filters):any for details.
Use replaceParams() to generate a filter which replaces parameters in a string. A function must be provided as argument, which is called to get values for the replacement.
Eric Bréchemier © 2011-2013, Some Rights Reserved Legal-Box SAS © 2010-2011, All Rights Reserved
BSD License http://creativecommons.org/licenses/BSD/
2013-09-10
lb. | Base String Template Module |
Functions | |
withValuesFrom([data]): function | Get a closure function that gets values of properties in data. |
replaceParams(getValue): function | Get a filter function to replace parameters in a string template. |
Get a closure function that gets values of properties in data.
This method is intended for use in combination with replaceParams(), to get a filter to replace parameters in a string template with values from given data:
var filter = replaceParams( withValuesFrom(data) )
data | object, optional, properties for parameter replacement, which may be nested in sections and subsections. Defaults to {}. Example: |
{ section: { subsection: { name: 'value' } } }
function, a closure wrapped around the given data, with the following signature:
Function: getDataValue(key): any Get the value of a property, possibly nested, in wrapped data. Parameter: key - string, the key identifying a property, which may be: * a string refering to the name of a property: 'name' * a dotted string for a nested property: 'section.name' Returns: * any, the value of corresponding property, if found * null otherwise
Get a filter function to replace parameters in a string template.
The parameters to replace are surrounded by ‘#’ characters, and allow the folowing characters in the name:
{ section: { subsection: { name: 'value' } } }
Parameters for which no value is found are left unreplaced.
getValue | function, a getter function returning values for the replacement of parameters: |
function(name): any
The name argument is the name of the parameter to replace. The getter value should return string values when a matching property is found, and null otherwise.
Function: filter(string): string Replace parameters in given string with values from wrapped getter. Parameters: string - string, the template string with parameters to replace Returns: string, a string computed from the template string by replacing named parameters with corresponding values returned by getValue()