0

I want to create a text template and set in it placeholders and fill this placeholders and use it in my page? like ..

var temp = '<div class="persons">';
temp += '<p> <span class="firstname">First Name :</span> <span> [firstname]</span> </p>';
temp += '<p> <span class="lastname">Last Name :</span> <span> [lastname]</span> </p>';
temp += '<p> <span class="tel">Telephone :</span> <span> [tel]</span> </p>';
temp += '</div>';

and change [firstname], [lastname], [tel] with my values and show in page? or any other idea?

j08691
  • 204,283
  • 31
  • 260
  • 272
AmirHossein
  • 367
  • 1
  • 8
  • 25
  • 1
    What have you tried? Also, you should take a look at existing JS template engines. – Florian Margaine Apr 25 '12 at 20:48
  • 1
    Use a sprintf / format type of function: see here http://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format/4673436#4673436 – Dominic Apr 25 '12 at 20:54

4 Answers4

3

Quick & dirty JS template engine:

// Create a matching object for each of your replacement
var matches = {
    "[firstname]": "fred",
    "[lastname]": "smith",
    "[tel]": "123456789"
}

// Loop through the keys of the object
Object.keys( matches ).forEach( function( key ) {
    // Replace every key with its value
    temp = temp.replace( key, matches[ key ] )
} )
Florian Margaine
  • 58,730
  • 15
  • 91
  • 116
  • 1
    I really like `Object.keys()`. Didn't know about it before. However in order to have an affect you have to store the result from `replace()` since it is not operating on the object itself. See here: http://jsfiddle.net/6vxKB/1/ – nuala Apr 26 '12 at 08:32
  • 1
    Oops, my bad, I'm editing for a working solution :). Be aware of `Object.keys` as it's not cross-browser although shimming it is a matter of [a few lines](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys) (the same goes for `forEach`). – Florian Margaine Apr 26 '12 at 08:36
2

You could simply replace them.

temp.replace("[firstname]", "fred").replace("[lastname]", "smith").replace("[tel]", "123456789");

Not sure if you are using jQuery Templates or not.

locrizak
  • 12,192
  • 12
  • 60
  • 80
2

Have you tried .replace() ?

http://jsfiddle.net/KL9kz/

nuala
  • 2,681
  • 4
  • 30
  • 50
1

Try something like this, very basic

function replaceall(tpl, o, _p, p_) {
    var pre = _p || '%',
        post = p_ || '%',
       reg = new RegExp(pre + '([A-z0-9]*)' + post, 'g'),
        str;
    return tpl.replace(reg, function (str, $1) {
        return o[$1];
    });
};
var temp = '<div class="persons">'+
        '<p> <span class="firstname">First Name :</span> <span> [firstname]</span> </p>'+
        '<p> <span class="lastname">Last Name :</span> <span> [lastname]</span> </p>'+
        '<p> <span class="tel">Telephone :</span> <span> [tel]</span> </p>'+
    '</div>',
    data = {
        'firstname':'Fede',
        'lastname':'Ghe',
        'tel' : '+00 012 3456789' 
    };
alert(replaceall(temp, data, '\\\[', '\\\]'));

be cafeful to escape Placeholders bounds and adjust inner regexp if needed hope it helps

fedeghe
  • 1,243
  • 13
  • 22