Skip to content

templateFormatters examples

jor77 edited this page Jun 9, 2016 · 3 revisions

Combine multiple json object's keys

Relying on the fact that you can pass the entire json object in this way

data-content="."

we could setup a template formatter that combine multiple keys of our json object, that way

	combValues: function (value, keys) {  
		var arrKeys = keys.split(',');
		var arrLength = arrKeys.length;
		var separator= arrKeys[0];
		var output = value[arrKeys[1]];
		for (var i = 2; i < arrLength; i++) {
			output = output + separator + value[arrKeys[i]];
		}			
		return output;
	}

The keys to be combined are passed by the data-format-options attribute as comma separated values :

data-format-options=" ,givenName,familyName"

Note that the first value is not a key but the separator you want to put between values returned by those keys... It could be a space or whatever you want (also more characters) but not a comma since comma is used by the function inside the formatter to distinguish values provided.

So an entire example would look like that :

<span data-content="." data-format="combValues" data-format-options=" ,givenName,familyName"></span>

and will output givenName value and familyName value separated by a space as this is the first parameter passed.

More will come 😄

Clone this wiki locally