J
John Passaniti
(Note: This is not the same message I posted a week or so ago. The
problem that prevented my previous attempt to work was a silly error in
the template system I was using. This is a problem involving variable
scope in JavaScript.)
I have a lot of code that generates HTML on the fly. This code has tags
with id attributes derived from variables. A small example:
blah('<span id="' + dev + '_' + mod + '">...</span>');
This is ugly, and in the typical more complicated cases rapidly becomes
an unreadable mess. What I want is a facility like in Perl, where I can
interpolate the value of variables. It might look like this:
blah('<span id="{dev}_{mod}">...</span>');
That is, whenever I see a string between curly brackets, I want to
replace that with the value of that variable.
So I wrote this function:
function XXX(s) {
return s.replace(
/\{([^}]+)\}/g,
function (dummy, v) {
return eval(v);
}
);
}
And it works... kinda. Here's an example:
x = 42;
y = "hello world";
z = {bobo: true};
alert(XXX("x,y,z.bobo: {x}, {y}, {z.bobo}"));
And yes, an alert box pops up with "x,y,z.bobo: 42, hello world, true"
displayed, as I wanted.
But there is a problem. This doesn't work:
function whatever(example) {
return XXX("example is {example}");
}
alert(whatever(42));
If you run this, it won't work. And the reason is clear: JavaScript is
evidently a lexically instead of dynamically scoped language. Okay, so
what I want is a way to evaluate in the context of the string I'm
passing in to XXX. I tried this slight change:
function XXX(s) {
return s.replace(
/\{([^}]+)\}/g,
function (dummy, v) {
return s.eval(v);
}
);
}
(The change: I tried "s.eval" instead of just "eval" in an attempt to
forced evaluation in the context of the string.) But it doesn't work.
So what I'm asking the experts here is how can I write my variable
interpolator so that it works as I've described. Or alternatively, if
an example of this exists in a JavaScript library, please point me to
that library.
problem that prevented my previous attempt to work was a silly error in
the template system I was using. This is a problem involving variable
scope in JavaScript.)
I have a lot of code that generates HTML on the fly. This code has tags
with id attributes derived from variables. A small example:
blah('<span id="' + dev + '_' + mod + '">...</span>');
This is ugly, and in the typical more complicated cases rapidly becomes
an unreadable mess. What I want is a facility like in Perl, where I can
interpolate the value of variables. It might look like this:
blah('<span id="{dev}_{mod}">...</span>');
That is, whenever I see a string between curly brackets, I want to
replace that with the value of that variable.
So I wrote this function:
function XXX(s) {
return s.replace(
/\{([^}]+)\}/g,
function (dummy, v) {
return eval(v);
}
);
}
And it works... kinda. Here's an example:
x = 42;
y = "hello world";
z = {bobo: true};
alert(XXX("x,y,z.bobo: {x}, {y}, {z.bobo}"));
And yes, an alert box pops up with "x,y,z.bobo: 42, hello world, true"
displayed, as I wanted.
But there is a problem. This doesn't work:
function whatever(example) {
return XXX("example is {example}");
}
alert(whatever(42));
If you run this, it won't work. And the reason is clear: JavaScript is
evidently a lexically instead of dynamically scoped language. Okay, so
what I want is a way to evaluate in the context of the string I'm
passing in to XXX. I tried this slight change:
function XXX(s) {
return s.replace(
/\{([^}]+)\}/g,
function (dummy, v) {
return s.eval(v);
}
);
}
(The change: I tried "s.eval" instead of just "eval" in an attempt to
forced evaluation in the context of the string.) But it doesn't work.
So what I'm asking the experts here is how can I write my variable
interpolator so that it works as I've described. Or alternatively, if
an example of this exists in a JavaScript library, please point me to
that library.