The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
SlightlyLoony
Tera Contributor

Yesterday I sort of glossed over a couple of things that appeared in our simple Jelly template sample:


<?xml version="1.0" encoding="utf-8"?>
<j:jelly>
<j:set/>
<j:while>

Test
<j:set/>
</j:while>
</j:jelly>

What I skipped were the two things that look like an expression surrounded by dollar signs and curly braces, like this one: ${jvar_loopCount + 1}. What's up with that name and that odd-looking syntax, my friend would like to know?

That, folks, is an example of JEXL, or Java EXpression Language — another open source technology from the Apache Foundation that is used by Jelly for various purposes. If you're familiar with the notion of macros (from C or assembly programming), these JEXL expressions are very similar. The ${ } syntax simply identifies the contents of the curly braces as a JEXL expression. If you write something like that anywhere in a Jelly template, when the template is processed the first step in processing each Jelly tag is to find all the stuff enclosed by dollar sign/curly braces within the tag and evaluate them as JEXL expressions. The result of each expression will be substituted for the entire dollar sign/curly brace construct. For example, consider this line in the sample Jelly template:

<j:set/>

The expression inside the curly braces (jvar_loopCount + 1) will be evaluated (computed) for each iteration of the while loop. On the first iteration, the value of jvar_loopCount is zero (because it was set to zero before executing the while loop), so the result of the JEXL expression evaluation is 1. That line of Jelly, after evaluating the JEXL, will look like this (internally only; you can't inspect this):

<j:set/>

Then the Jelly engine will simply set the jvar_loopCount variable to one. On the next loop iteration, that JEXL expression will be re-evaluated — and this time the result will be two.

It bears repeating that a JEXL expression (in the curly brace syntax) can be anywhere in a Jelly template, so long as before processing the JEXL, the Jelly template is still valid XML. In particular, there's no requirement that it appear in a string literal, as we've done in our example. Check out this small modification to our sample Jelly template:

<?xml version="1.0" encoding="utf-8"?>
<j:jelly>
<j:set/>
<j:while>

Test ${jvar_loopCount}
<j:set/>
</j:while>
</j:jelly>

That is still valid XML, even though our new JEXL expression isn't inside of a string literal. Instead, it is just text inside the HTML tag. While there are many places where you can't put a JEXL expression (because it would mess up the XML syntax), there are many useful places where you can...

The JEXL expression language is actually quite powerful, as you can see if you read the reference to it's syntax. A few features of JEXL can't be used in Jelly because they conflict with either XML syntax or the dollar sign/curly brace syntax, but most of it is wide open for you.

One last thing: if you look through the UI pages and other Jelly templates that come with your ServiceNow instance, you may notice something that looks an awful lot like JEXL, except that instead of curly braces it uses square braces. For example, you might see something like $[jvar_x lt 42]. Well, that is JEXL, but with twist that is one of the ServiceNow enhancements to Jelly. For now, don't worry about why it's different (we'll cover that in detail in a later post) — just know that it is JEXL, just like the curly braces version...