The Now Platform® Washington DC release is live. Watch now!
The ServiceNow platform renders HTML based on data it retrieves from its database. Often we want to make decisions about what HTML to render based on that database information. The Jelly tags (its statements) that let you do that are its conditional tags, and they are the subject of today's post. Our little helper at right can't wait for some more Jelly, conditional or not!
In this post I'm going to show you how Jelly lets you choose whether particular HTML gets rendered. For this purpose, we're going to use a silly little exercise: if a variable named jvar_employee is true, we'll render some HTML that shows the name as an employee's name; if it's false, we'll render it as an alien's name. For now, don't worry about how the variables we're using have values set in them in real; we'll cover that in later posts — just concentrate on understanding how Jelly's conditional tags work. For the purpose of our exercise, we're just setting the variables to a constant value.
Probably the most common conditional tag you'll see is the if tag. Here's what our exercise problem looks like using this basic conditional tag:
<?xml version="1.0" encoding="utf-8"?>
<j:jelly>
<j:set/>
<j:set/>
<j:if>
Employee:$[SP]${jvar_name}
</j:if>
<j:if>
Alien:$[SP]${jvar_name}
</j:if>
</j:jelly>
if (employee)
output('employee stuff');
else
output('alien stuff');
if (employee)
output('employee stuff');
if (!employee)
output('alien stuff');
<?xml version="1.0" encoding="utf-8"?>
<j:jelly>
<j:set/>
<j:set/>
<j:choose>
<j:when>
Employee:$[SP]${jvar_name}
</j:when>
<j:otherwise>
Alien:$[SP]${jvar_name}
</j:otherwise>
</j:choose>
</j:jelly>
if (x < 0)
// do stuff...
else if (x < 5)
// do other stuff...
else if (x < 100)
// do even other stuff...
else
// do desperately different stuff...
<?xml version="1.0" encoding="utf-8"?>
<j:jelly>
<j:set/>
<j:set/>
<j:switch>
<j:case>
Employee:$[SP]${jvar_name}
</j:case>
<j:default>
Alien:$[SP]${jvar_name}
</j:default>
</j:switch>
</j:jelly>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.