{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":42187,"path":"platform-user-interface/content-management-system/r_ContentManagementAndJelly.md","title":"Content Management and the Apache Jelly engine","bundle":null,"doc_type":null,"release":"australia","body":"\n# Content Management and the Apache Jelly engine\n\nApache Jelly is a Java-based and XML-based scripting and processing engine for turning XML into executable code.\n\nThe Apache Jelly engine closely resembles XML and should be comfortable for developers familiar with JavaScript, XML, XHTML, or HTML. In the ServiceNow instance, the Apache Jelly engine renders items such as forms, lists, and UI Pages. Apache Jelly code renders well within a dynamic content block, but can have issues when used in static blocks. You can use Jelly tags, calls, and statements, but HTML acts just like XHTML.\n\n```\n<?xml version= \"1.0\" encoding= \"utf-8\" ?>\n<j:jelly trim = \"false\" xmlns:j = \"jelly:core\" xmlns:g = \"glide\" xmlns:j2 = \"null\" xmlns:g2 = \"null\" >\n \n\t<j:if test = \"${current_page.getName()=='Solutions'}\" >\n\t<h1 class = \"page_name\" > <b> <a href = \"solutions.do?\" title = \"${gs.getMessage('Solutions')}\" >${gs.getMessage('Solutions')}</a> </b> </h1>\n\t<p class = \"page_description\" >\n\t \t    ${current_page.getDescription()}\n\t</p> <br />\n\t</j:if>\n\t<j:if test = \"${current_page.getName()=='IT 3.0'}\" >\n\t   <h1 class = \"page_name\" > <b> <a href = \"solutions.do?\" title = \"${gs.getMessage('Solutions')}\" >${gs.getMessage('Solutions')}</a> </b> | ${current_page.getName()}</h1>\n\t   <p class = \"page_description\" >\n\t \t    ${current_page.getDescription()}\n\t\t</p> <br />\n\t\t</j:if>\n\t\t</j:jelly>\n```\n\nEnsure that all tags are closed. If the tag is not a naturally closing tag, then place a forward slash before the end bracket. For example, a `<BR />` or an `<IMG src=\"cms.png\" />`.\n\nIf you are unfamiliar with Jelly scripting, review these topics:\n\n-   Jelly tags\n-   Extensions to Jelly syntax\n-   Jelly escaping types\n\nInclude the following tag with all Apache Jelly scripts.\n\n```\n<j:jelly trim= \"false\" xmlns:j= \"jelly:core\" xmlns:g= \"glide\" xmlns:j2= \"null\" xmlns:g2= \"null\" >\n```\n\nThe tag looks complex, but keep the following information in mind.\n\n-   Apache Jelly script uses multiple namespaces.\n-   There are two types of prefixes in tags: j and g. The j prefix is used for tags that are natively part of Apache Jelly. The g prefix is used for tags that the ServiceNow platform created and is using for platform purposes.\n\nThe j2 and g2 prefixes are just like j and g, except that they are processed in a second phase. The Apache Jelly script parser runs through each j and g tag respectively. For example:\n\n```\n<j:jelly trim=\"false\" xmlns:j=\"jelly:core\" xmlns:g=\"glide\" xmlns:j2=\"null\" xmlns:g2=\"null\">\n\t<j:set var=\"jvar_phase1\" value=\"Hello\" />\n\t<j2:set var=\"jvar_phase2\" value=\"World\" />\n\t${jvar_phase1} $[jvar_phase2]\n</j:jelly>\n```\n\nIn phase 1, the parser runs through all the j and g tags. It then caches the result. Before it runs the second phase, it takes the j and g namespaces and moves the namespaces to the second phase. It looks something like the following code.\n\n```\n<j:jelly trim=\"false\" xmlns:j=\"jelly:core\" xmlns:g=\"glide\" xmlns:j2=\"jelly:core\" xmlns:g2=\"glide\">\n\t<j2:set var=\"jvar_phase2\" value=\"World\" />\n\tHello $[jvar_phase2]\n</j:jelly>\n```\n\nFor subsequent calls of this script, only phase 2 is parsed.\n\n**Note:** If you plan to use phase 2 Jelly tags \\(g2 and j2\\) on the Content Type \\[content\\_type\\] or Dynamic Content \\[content\\_block\\_programmatic\\] tables, select the **Two phase** option on the content form.\n\nAnother example is to create a report of all open incidents assigned to each group. For this purpose, you could use a report and save time, but it is a good example for learning Jelly. Start with the Jelly tag:\n\n```\n<j:jelly trim= \"false\" xmlns:j= \"jelly:core\" xmlns:g= \"glide\" xmlns:j2= \"null\" xmlns:g2= \"null\" >\n</j:jelly>\n```\n\nFirst, you need a list of open incidents. Use a `g2:evaluate` tag. The evaluate tag runs the script. Anything inside the tag is parsed like a business rule, so, for example, you can call global business rules, script includes, and gliderecord.\n\n```\n<j:jelly trim=\"false\" xmlns:j=\"jelly:core\" xmlns:g=\"glide\" xmlns:j2=\"null\" xmlns:g2=\"null\">\n\t<g:evaluate var=\"jvar_groups\" object=\"true\">\n\t\tvar now_GR = new GlideRecord(\"sys_user_group\");\n\t\tgr.orderBy('name');\n\t\tgr.query();\n\t\tgr;\n\t</g:evaluate>\n</j:jelly>\n```\n\nThis script is in phase 1 because frequent changes to incident assignment groups are not expected. Also notice the `var` attribute on the evaluate tag. This attribute specifies what variable is set from this block. At the end of the script, there is a `gr` on a line by itself. That last line is what sets the variable.\n\nYou can omit the `jvar_groups` variable, but then all the variables in the evaluate tag become Apache Jelly variables. The `object=true` specifies that the variable is not a primitive data type. If `object=true` is omitted, the script would break because `jvar_groups` would only be able to hold items like integers and strings.\n\nAfter the evaluate tag, loop through these groups and find the incidents for each one.\n\n```\n<j:jelly trim=\"false\" xmlns:j=\"jelly:core\" xmlns:g=\"glide\" xmlns:j2=\"null\" xmlns:g2=\"null\">\n\t<g:evaluate var=\"jvar_groups\" object=\"true\">\n\t\tvar now_GR = new GlideRecord(\"sys_user_group\");\n\t\tgr.orderBy('name');\n\t\tgr.query();\n\t\tgr;\n\t</g:evaluate>\n\t<table>\n\t\t<tr>\n\t\t\t<th>Name</th>\n\t\t\t<th>Incidents</th>\n\t\t</tr>\n\t\t<j:while test=\"${jvar_groups.next()}\">\n\t\t\t<tr>\n\t\t\t\t<td>${HTML:jvar_groups.getValue('name')}</td>\n\t\t\t\t<td></td>\n\t\t\t</tr>\n\t\t</j:while>\n\t</table>\n</j:jelly>\n```\n\nYou can include normal XML in the Apache Jelly script at any time. Since there is no namespace, the Apache Jelly script does not try to parse the XML tags. Notice the `j:while` loop. It is a normal while loop and can iterate through a GlideRecord object. Also notice that you output a value with `${HTML:jvar_groups.getValue('name')}`. Here are the important elements:\n\n-   The outer brackets, `${}`, specify the output of the variable and the phase in which the variable is output: `${}` means first phase, `$[]` means second phase.\n-   HTML before the expression is for escaping the output. The expression `jvar_groups.getValue('name')` is being escaped for HTML. For other types of escaping, there are JS \\(Javascript\\), NS \\(No Script\\), and some other options.\n\nTo select only one record and not iterate through many records, the code looks like the following example:\n\n```\n<j:jelly trim=\"false\" xmlns:j=\"jelly:core\" xmlns:g=\"glide\" xmlns:j2=\"null\" xmlns:g2=\"null\">\n\t<g:evaluate var=\"jvar_groups\" object=\"true\">\n\t\tvar now_GR = new GlideRecord(\"sys_user_group\");\n\t\tgr.orderBy('name');\n\t\tgr.query();\n\t\tgr;\n\t</g:evaluate>\n\t<j:if test=\"${jvar_groups.next()}\">\n\t\tWe found ${HTML:jvar_groups.getValue('name')}\n\t</j:if>\n</j:jelly>\n```\n\n-   **[Content management and Jelly code examples](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/platform-user-interface/content-management-system/r_CodeExamples.md)**  \nCode examples\n\n**Parent Topic:**[Content blocks](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/platform-user-interface/content-management-system/c_ContentBlocks.md)\n\n**Related topics**  \n\n\n[Configure a content block]()\n\n[View CMS block tags]()\n\n[Types of content blocks]()\n\n"}}