calling a snippet within a page

I am using dynamic publishing and have a rendered snippet that I would like to include in my XML. How can I call a snippet of myself within a page of myself?

Sharyn,

I just answered this question the other day, but I’m not sure where (and it may have been internal to Percussion rather something I can point you to here)

The basic algorithm is to:

  1. Clone the current Assembly Item
  2. Replace the Template in the Assembly item with the one you want
  3. Call the Assembly Service to assemble the item.

The Assembly Result has a getResultData() method that returns you the assembled template as a Byte Array.

I know we’ve done this a couple of times in the past, but there is some disagreement about whether this can be done without writing a new JEXL function. I’ll have to go dig through some of the old implementations (in source control) to find out how we did it.

Since I’ve been asked this a few times in the last week or so, I suggest that if a new function is required, I’ll add it to the PSO Toolkit and post a further reply. If this can be done with functions we already have, I’ll let you know.

The custom assembler code (in DynaPub) does something very much like this, BTW.

Dave

Here’s an example of how to do this:

Let’s say I want to include the contents of the template named “rffSnCallout” in my new template.

In the bindings of my template (the syntax is slightly different in Velocity)

$calloutTemplate = $sys.asm.findTemplateByName("$rffSnCallout")
$calloutItem = $sys.assemblyItem.clone()
               $calloutItem.setTemplate($calloutTemplate)
$resultList = $sys.asm.assemble($user.psoListTools.asList($calloutItem))
$resultData = $user.psoBase64.encode( $resultList.get(0).getResultData())

The only tricky bits are that the IPSAssemblyService (which you can access in a template as $sys.asm) assemble() method wants a “List” of IPSAssemblyItem, so you need the $user.psoListTools.asList() to make a list. The result is also a list (which is why we need .get(0) on the next line.

In a real world template, it would probably be a good idea to check if these lists are non-empty before operating on them (unless you just want the default error behavior).

The other issue is that the IPSAssemblyResult.getResultData() method returns a byte array. If you’re doing DB publishing or something like that, you’re likely to need it Base64 encoded. This is what is shown in my example.

I hope this helps

Dave

Thanks for the reply. This works but it is not rendering the final page instead it is outputting the actual template code with all the variables and conditions in the XML page “as is” in the snippet template.

#displayfield(“displayTitle”)

#if ($rx.asmhelper.getSingleParamValue($sys.params, “sys_context”) == ‘0’)

Is there anyway to return the actual previewable piece of the template?

Also we are not using the dynapub for file publishing out the final piece. Everything will be database published so we are only using the XML to publish. We decided to bypass that option in the dynapub during the JAD.

Sharyn,

Could you post your template code again, and enclose it in CODE or HTML tags (use the buttons in the VBulletin editor).

To get the document in text form, use one of these methods:


$result = $sys.asm.assemble($user.psoListTools.asList($calloutItem)).get(0)
$resultText = $result.toResultString()
$resultBody = $rx.doc.extractBody($result)

$resultText will be the entire snippet (including the <html> and <head> tags).

$resultBody will be just the body.

If you’re going to put in XML (and it can contain formatting), Base64 is the way to go.

I’ve tested these, so I know that they work.

Dave

Interesting…not sure what happened but before it was bringing back the entire template as is but now it is bring it back as encoded or as text with the new call. This is my final code in the template that is now working.

NOTE: that in the original sample code the setTemplate was setting itself to $calloutItem instead of $calloutTemplate .


#if ($contentType == "Article")
	
<renderedPages>
#set($calloutTemplate = $sys.asm.findTemplateByName("bcPgTestPaginator"))
#set($calloutItem = $sys.assemblyItem.clone())
$calloutItem.setTemplate($calloutTemplate)
#set($resultList = $sys.asm.assemble($user.psoListTools.asList($calloutItem)))
#set($resultData = $user.psoBase64.encode($resultList.get(0).getResultData()))
$resultData

#set ($result = $sys.asm.assemble($user.psoListTools.asList($calloutItem)).get(0))
#set ($resultText = $result.toResultString())
#set ($resultBody = $rx.doc.extractBody($result))
$resultBody
</renderedPages>
 #end

shortened version of the end result in preview:



- <renderedPages>
  +CjwvYXJ0aWZhY3Q+Cg== 
- <![CDATA[ 	 Reading any book is a soothing way to send your baby off to dreamland — but stories about going to bed, sleeping, or dreaming are particularly suitable for bedtime, and can even help your child understand and accept
  ]]> 
  </renderedPages>