Jelly: Basics, on the Way to Jelly Ninja-hood...


Now that you have some idea what Jelly actually is, it's time for us to stick a toe into the Jelly jar. Yesterday I showed you this simple little Jelly template:

<?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_loopCount" value="0"/>
    <j:while test="${jvar_loopCount lt 3}">
        <p>Test</p>
        <j:set var="jvar_loopCount" value="${jvar_loopCount + 1}"/>
    </j:while>
</j:jelly>

Today I'm going to tell you what all that stuff actually means. This is Jelly 101, folks.

The first line of that template is just a standard XML document header, not specific to Jelly at all.

<?xml version="1.0" encoding="utf-8" ?>

The next line (and it's closing tag) identify this as a Jelly template. Every Jelly template must include this tag as the outermost tag, and all of the attributes (strictly speaking, pseudo-attributes) starting with xmlns: must be in it. There are other attributes possible for this tag, but for now they don't matter. This line defines a few XML namespace prefixes used within Jelly documents. In our case, there is the commonly used “j” prefix for standard Jelly tags, plus three prefixes (“g”, “j2”, and “g2”) for tags that ServiceNow has added. The jelly tag (which is a standard Jelly tag) itself uses the namespace prefix “j”; that's why it appears as j:jelly:

<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
</j:jelly>

It isn't actually important to understand all those details – you can get away with thinking of this as a magic incantation that's required to create a Jelly template.

Now we're down to the meat – the part of our Jelly template that actually does something!

    <j:set var="jvar_loopCount" value="0"/>
    <j:while test="${jvar_loopCount lt 3}">
        <p>Test</p>
        <j:set var="jvar_loopCount" value="${jvar_loopCount + 1}"/>
    </j:while>

To help you see what's going in that mess, here's a JavaScript program that is line-for-line the equivalent of that Jelly – but easier to read, as it's not in an XML document:

    var jvar_loopCount = 0;
    while (jvar_loopCount < 3) {
        output('<p>Test</p>');
        jvar_loopCount = jvar_loopCount + 1;
    }

Those five lines of Jelly are just making a little while() loop, and outputting our boring HTML <p>Test</p> three times. The reason it doesn't necessarily look like a programming language is all the stuff they had to do to make it possible to express this “program” in an XML document.

Take the first line, for example. In JavaScript, var jvar_loopCount = 0; works very well – but for XML, not so much. The Jelly designers translated that into a set tag (to indicate that we're setting a variable to some value) with a var attribute to name the variable we're setting, and a value attribute to indicate what value we're setting it to.

The other lines are similarly more familiar programming language constructs translated into a form that can be expressed in XML. Understanding this is crucial to understanding how Jelly works – it's worth some practice, which will make it much easier for you to look at (and make sense of!) Jelly templates. Also, just like other programming languages, the use of whitespace (especially for indentation) helps make Jelly templates understandable. I could have written the Jelly and JavaScript like this:

    <j:set var="jvar_loopCount" value="0"/> <j:while test="${jvar_loopCount lt 3}"> <p>Test</p>
    <j:set var="jvar_loopCount" value="${jvar_loopCount + 1}"/></j:while>
    var jvar_loopCount = 0; while (jvar_loopCount < 3) {output('<p>Test</p>');
    jvar_loopCount = jvar_loopCount + 1;}

But I sure find it easier to read and understand it the way I first wrote it!

Enough basics for now. If you'd like to investigate further, here is the online documentation for the standard Jelly tags we used: jelly, set, and while.

Tomorrow: one step closer to Jelly ninja-hood!


Comments

What's the story on this

What's the story on this output() function? I didn't know that existed, nor have I come across it in the SN Wiki Jelly article(s).

__________________

*** Please remember to mark your question as "Answered" ***



TONY FUGERE  |  Service Delivery Manager
tony.fugere@cloudsherpas.com  |  direct  (858) 356-5696 x212
ServiceNow Certified Admin, Implementation Specialist, Trainer

ServiceNow Partner

It's not a real thing at

It's not a real thing at all, Tony – just my feeble attempt to show you what the equivalent JavaScript might look like IF you could actually use JavaScript to do this. But you can't really use it...

Thanks !!!

Thanks !!!

Encoding like that works for

Encoding like that works for a single instance, but there is a boolean property glide.ui.escape_text that, depending on its value, will render that incorrectly. If that property is true, the ampersand itself would end up getting escaped, and instead of a < on the other end, you'll actually have &lt; as your operator.

I may be stepping on Tom's toes for tomorrow post, but there is a Jelly substitution you can use if you really need to use a less than symbol, that will work regardless of the value of the property: ${LT} will evaluate to a less than symbol.

ServiceNow EmployeeServiceNow Moderator

To get around the fact that

To get around the fact that Jelly really doesn't like the normal less-than symbol in a comparison, I like to switch the values and use >= instead:

<j:while test="${3 >= jvar_loopCount}"> 

ServiceNow EmployeeServiceNow Moderator

Another option is to HTML

Another option is to HTML encode the < and > i.e. write &lt; and &gt;

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.