Variable set in snippet inserted via ephox visible to global template

Hi

Is there any way to make a variable set in a snippet template that is inserted into an ephox field visible to the global page template? It seems that nothing is passed outside of ephox field.

We have snippet templates that are inserted inline to display things like video, image galleries etc. These use javascript and I would like to set a variable in the snippet used to insert these items which I can test for in the global template and if it is set then include the necesary .js files otherwise not include them

Kevin

Good afternoon Keevan.

I’ve implemented something similar to this previously. Even though the “inline” slots are not listed with the other available slots (e.g in the table editor), the items added to an inline slot can be accessed like any other slot.

First you need to determine whether you’re inserting the items in-line as inline links or inline templates, then call the appropriate slot (sys_inline_link or sys_inline_variant) in the global template, test for your variable, then output your necessary .js files.

I used the following code to walk through the items added as in-line templates in a body field, and render them in as list elements (the li tag was included in the definition popup snippet). There’s code here that checks for a particular content type, but you could use a field value on the $relresult.Node just as easily.

#initslot("sys_inline_variant"  "template=Sn_DefinitionPopup")
  #if($sys.currentslot.relresults.size() > 0)
  <ul>
     #foreach( $relresult in $sys.currentslot.relresults )
        #set($contenttypename = $relresult.Node.definition.internalName)
        #if($contenttypename == 'Definition')
           #slotItem($relresult)
        #end
     #end
  </ul>
  #end
#endslot("sys_inline_variant")

You could also use the same approach in the header of your Global template with a simple check to conditionally render the .js call.

#initslot("sys_inline_variant"  "template=Sn_DefinitionPopup")
  #if($sys.currentslot.relresults.size() > 0)
  <ul>
     #set($include = "no")
     #foreach( $relresult in $sys.currentslot.relresults )
        #set($myvariable = $relresult.Node.getProperty("fieldname").String)
        #if($myvariable == 'Desired value')
           #set($include = "yes")
        #end
     #end
     #if($include =='yes')
           <script call goes here >
     #end
  </ul>
  #end
#endslot("sys_inline_variant")

A few things to note. The in-line slots are not field specific. If you have more than one RichText field on a given template, the slot call above would include any item added to the called slot across ALL fields. This is why I usually put a check in to make sure I’m only rendering for the items that I’m interested in.

Hope this helps.

M.

Another way to do it is to use javascript to check the contents of the page. For instance, you could check for existence of a particular class (ie. using jquery) and if that class is found, then dynamically call the javascript necessary.

For example using jquery:


$(document).ready(function(){
if($(".myspecialclassforavideoorsomethinglikethat").size()>0){
$.getScript("myspecialjavscriptfile.js", function(){});
}
}

You could choose not to use the getScript function and instead, add the script as an element to the header (this allows the client to cache the file):


$('head').prepend('<script type="text/javascript" src="myspecialjavascriptfile.js"></script>');

One benefit of doing it this way, is that the processing is done on the client browser (as opposed to the Rhythmyx server on preview and publish).

Hi Michael

I tried your example which works when used in a page template but when used in the global template the only item in the sys_inline_variant slot is the page.

I guess once it has been extracted from the inline slot I could use $sys.assemblyItem.Bindings.put(’$include_media’, ‘yes’) to make the variable available to the global template. This could also be used to make things work when the inline template is inserted inline to one content item then that item is inserted into another item. It just means adding code to a lot more pages

Thanks jitendra for your JS suggestion but I prefer a rhythmyx solution

Thanks
Kevin