We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

How to pass jelly variables between dynamic blocks

kevinanderson
Giga Guru

I am in the process of updating some of our CMS pages, and I am using dynamic blocks as the core of this work.

I am using the following line to call other dynamic blocks into parent blocks:

<g:content_block type="content_block_programmatic" id="[dynamic block sys_id]"/>

I have 5 dynamic blocks that are nearly the same html format, with slightly different data. I would like combine these dynamic blocks into a   single entity and then pass the necessary data to each block in a   loop, similar to the following (this does not work):

<g:evaluate>

              /*   define data used to build navigation elements       */

              var categories = [

                      {

                              'name':'common-requests',

                              'nav_title': 'Common Requests'

                      } /* define more array entries */

            ];

  </g:evaluate>

/* loop over categories array and read up title and category name */

<j:forEach items="${categories}" var="jvar_category">  

    <g:evaluate jelly="true">  

                  var name = jelly.jvar_category.name;  

                  var title = jelly.jvar_category.nav_title;

    </g:evaluate>

/** want to be able to access variables "name" and "title" from inside this dynamic block */

<g:content_block type="content_block_programmatic" id="e93497172bee8e00663d1c9069da15c3"/>

</j:forEach>

This would work similar to a template in any other server-side web language...but I am unable to figure out how to pass the data I have between these two dynamic blocks.

Thanks for the help

3 REPLIES 3

kevinanderson
Giga Guru

http://wiki.servicenow.com/index.php?title=Jelly_Tags#macro_invoke_tag


Re: Re: Dynamic Content Block Values


Solution was to transition the child dynamic block to a UI macro:



UI Macro:   test_ui_macro1


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


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


      <div style="color: red;">${jvar_param1} </div>


</j:jelly>




Dynamic block   parent_block


<?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_variable1" value="This is a sample macro variable."/>


      <g:macro_invoke macro="test_ui_macro1" param1="${jvar_variable1}" />


</j:jelly>


kevinanderson
Giga Guru

The session can also be used to pass data between dynamic blocks.   I am calling a child dynamic block from a parent dynamic block, and I cant use a macro b/c I need the two -phase component available in a dynamic block.




http://wiki.servicenow.com/index.php?title=GlideSession#gsc.tab=0



in the parent dynmaic block:



<g:evaluate jelly="true">


    var session = gs.getSession();


    session.putClientData('my-session-data1', jelly.jvar_ritm_sysid);


</g:evaluate>



and in the child dynamic block


<g:evaluate var="jvar_ritm_sysid">

            var session = gs.getSession();


            var ritm_sysid = session.getClientData('my-session-data1');


        ritm_sysid;

      </g:evaluate>


meia_ahaesy
Mega Expert

I define the variable in the layout as a global variable so its shared. You can also pass it via a function call, but each function would need to have a unique name, or store/update it in a hidden input element, and retrieve the value from the form element



Lazy way: Declare the jelly var in the layout.
Not-Lazy way: Pass it to unique functions, between the two, or do the form element update.