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

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>
<j:set/>
<j:while>

Test
<j:set/>
</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>
</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/>
<j:while>

Test
<j:set/>
</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('
Test');
jvar_loopCount = jvar_loopCount + 1;
}

Those five lines of Jelly are just making a little while() loop, and outputting our boring HTML

Test

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/> <j:while>
Test
<j:set/></j:while>


var jvar_loopCount = 0; while (jvar_loopCount < 3) {output('
Test');
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!

6 Comments