Reputation: 867
I am trying to make something like
var stringUrlTemplate = home.url + '/data/[param1]/[param2]/' + endurl;
And than something like in some loop:
outputUrlItem = print(stringUrlTemplate, someVarialbe, someVariable);
Basically what C/C++ can do.
I cannot use "/data/${param1}/${param2}/" because a string is served by server separetly from JavaScript.
Upvotes: 1
Views: 290
Reputation: 58
This package allow reusable template literals - reuse-template-tag, maybe it can help, Here is example from their docs:
const reuseTemplateTag = require('reuse-template-tag')
const template = reuseTemplateTag`
- Hey ${1} How are You?
- Are You ok ${1} ?
- Yes, thanks ${'me'}!
`
console.log(
template({
1: 'Joe',
me: 'Andy'
})
)
// Or provide sequence of values with Array
console.log(
template(['Joe', 'Molly', 'Susan'])
)
Upvotes: 1
Reputation: 133403
You can use placeholder which can be replaced. Here is an example
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined' ?
args[number] :
match;
});
};
}
var stringUrlTemplate = '/data/{0}/{1}/';
console.log(stringUrlTemplate.format('data1', 'data2'))
OR,
function printf() {
var args = Array.prototype.slice.call(arguments, 1);
var str = arguments[0];
return str.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined' ?
args[number] :
match;
});
};
var stringUrlTemplate = '/data/{0}/{1}/';
console.log(printf(stringUrlTemplate, 'data1', 'data2'))
Upvotes: 2