This is MooTools code:
var myString = "{subject} is {property_1} and {property_2}.";
var myObject = {subject: 'Jack Bauer', property_1: 'our lord', property_2: 'savior'};
myString.substitute(myObject);
Does jQuery has this method or similar?
This is MooTools code:
var myString = "{subject} is {property_1} and {property_2}.";
var myObject = {subject: 'Jack Bauer', property_1: 'our lord', property_2: 'savior'};
myString.substitute(myObject);
Does jQuery has this method or similar?
Share Improve this question edited Oct 16, 2019 at 1:25 brasofilo 26.1k15 gold badges93 silver badges186 bronze badges asked Apr 12, 2010 at 10:04 zjm1126zjm1126 66.8k86 gold badges179 silver badges222 bronze badges 1- planabc/2011/05/31/simple_javascript_template_substitute github./yui/yui3/blob/master/src/substitute/js/substitute.js Here is YUI's solution. – user777019 Commented Nov 13, 2011 at 5:07
4 Answers
Reset to default 11No, but there's nothing preventing you from adding it yourself:
jQuery.substitute = function(str, sub) {
return str.replace(/\{(.+?)\}/g, function($0, $1) {
return $1 in sub ? sub[$1] : $0;
});
};
// usage:
jQuery.substitute('{foo}', {foo:'123'});
Try this plugin https://github./trix/nano , the source is just a few lines
/* Nano Templates (Tomasz Mazur, Jacek Becela) */
(function($){
$.nano = function(template, data) {
return template.replace(/\{([\w\.]*)\}/g, function (str, key) {
var keys = key.split("."), value = data[keys.shift()];
$.each(keys, function () { value = value[this]; });
return (value === null || value === undefined) ? "" : value;
});
};
})(jQuery);
You can use dot notation {user.name} , just simple and powerful.
The $.nano
answer threw me for a loop because it errors if you have any typos in your template dot notation, and furthermore it doesn't allow all legal characters like a['foo bar']
so below is my version as a $.substitute
plugin:
/*
* JQuery Substitute method allows for simple templating using JS Object dot notation.
* Contribute link: https://gist.github./danielsokolowski/0954fc2a767f441720b9
*
* @param strTemplate - string contain the replacement tokens
* @param objData - an Object represetnting your replacmenet values
*
* Example:
* var strTemplate = 'Hello {user.name}'
* var strDatra = {'user': 'Daniel Sokolowski'}
* alert($.substitute(strTemplate, objData)); // outputs 'Hello Daniel Sokolowski'
*
*/
$.substitute = function(strTemplate, objData) {
return strTemplate.replace(/\{([^{}]*)\}/g, function(math, subMatch1) {
try {
var keys = subMatch1.split(".");
var value = objData[keys.shift()]; // return first element and update the original array
while (keys.length !== 0) { // keep returning properties
value = value[keys.shift()]
}
return String(value);
} catch (err) { // return any errors as a string
return String(value);
}
});
};
There are some plugins that share a similar syntax to the String.Format method in .NET.
This one leverages the jQuery Validate plugin (monly found on CDNs).
Example:
$("button").click(function () {
var str = "Hello {0}, this is {1}";
str = jQuery.validator.format(str, "World", "Bob");
alert("'" + str + "'");
});
The second plugin is named .NET Style String Format.
Example:
var result = $.format("Hello, {0}", "world");
These may not be exactly what you're looking for, but they may be useful.