The Now Platform® Washington DC release is live. Watch now!
I've briefly mentioned (but didn't really explain) the two types of variables one can find in Jelly templates: Jelly variables and JavaScript variables. My colleague Burton (seen at right while creating a particularly challenging Jelly template) wants to know things. "Why," he asks "would I use a Jelly variable instead of a JavaScript variable? Or vice versa? And whatever those differences are, why on earth didn't you blithering idiots just make it all work with one or the other? Do I have to do it all for you?"
Well, Burt, it's like this:
First, wash your face, bud. You're about to make a serious mess of your keyboard and mouse.
The key to sorting all this out is to realize that there are three different places where variables get used — and of course, what works where is different in each case:
Here's an example of several easy-to-make mistakes regarding Jelly and JavaScript variables:
<pre style="margin-left:20px;line-height:1;color:FireBrick;">
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<j:set var="jvar_alpha" value="alpha" />
<g:evaluate jelly="true">
var colors = ['Red', 'Black', 'Blue', 'Brown', 'CadetBlue', 'DarkGreen', 'DeepPink'];
var jvar_test = 'test';
jelly.jvar_alpha = 'ALPHA';
jelly.jvar_beta = 'BETA';
</g:evaluate>
<j:forEach items="${colors}" var="color">
<g:breakpoint/>
<p>Color: $[SP] <span style="color:${color};"> ${color}</span></p>
</j:forEach>
<p>test: ${jvar_test}</p>
<p>alpha: ${jvar_alpha}</p>
<p>beta: ${jvar_beta}</p>
</j:jelly>
</pre>
This goes horribly wrong in several ways. We'll take 'em one at a time:
Color: $[SP] ${color}
), we use that variable name in JEXL expressions — and those try to look up the value in a JavaScript variable (because the variable name doesn't start with jvar_). This is a good example of a bad thing that can happen because you didn't start a Jelly variable's name with jvar_.Just for fun, here's the same thing with all the problems fixed. Note the use of JEXL expressions to move JavaScript values into Jelly variables:
<pre style="margin-left:20px;line-height:1;color:FireBrick;">
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:evaluate jelly="true">
var colors = ['Red', 'Black', 'Blue', 'Brown', 'CadetBlue', 'DarkGreen', 'DeepPink'];
var test = 'test';
var alpha = 'ALPHA';
var beta = 'BETA';
</g:evaluate>
<j:set var="jvar_test" value="${test}"/>
<j:set var="jvar_alpha" value="${alpha}"/>
<j:set var="jvar_beta" value="${beta}"/>
<j:forEach items="${colors}" var="jvar_color">
<g:breakpoint/>
<p>Color: $[SP] <span style="color:${jvar_color};"> ${jvar_color}</span></p>
</j:forEach>
<p>test: ${jvar_test}</p>
<p>alpha: ${jvar_alpha}</p>
<p>beta: ${jvar_beta}</p>
</j:jelly>
</pre>
I hope that's quite enough on this subject, Burt! Did you wash your face yet?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.