You could also do sth like this:

String.prototype.templatize = function(values)
{
        return this.replace(/{\$([^}]+)}/g, function (full_match, group_match)
                {
                        return values[group_match];
                });
}

and use it like this:

var tpl = "Flight {$airport_from} -> {$airport_to}.<br/>Departure
{$outbound_departure}, Arrival {$outbound_arrival}";

var values = {
        'airport_from': 'SFO',
        'airport_to': 'LON',
        'outbound_departure': '17:30',
        'outbound_arrival': '10:00'
};

var html = tpl.templatize(values)


You would just need to adapt the regex to your # instead of mine and
tuck your vars in an object.


Matt


On 7/27/07, weepy <[EMAIL PROTECTED]> wrote:
>
> Sick and tired of string manipulation in javascript ?  I was....
>
> Try this nice little function :
>
> $s = function(s) {
>   p = s.replace(/#{/g, "' + eval(").replace(/}/g, ") + '")
>   p = "'" + p + "'"
>   return eval(p)
> }
>
> OK it's not exactly bullet-proof, but it's the concept that counts
> here.
>
> So now you can do :
>
> a="Jonah"
> b=30
>
> $s("#{a} is #{b} years old")
>    ==> "Jonah is 30 years old"
>
> Obviously any stray '#{' and '}' sequences will break it...
>
>

Reply via email to