Getting all the content objects on a page


We have been asked repeatedly how to get a list of all the content objects that have been placed on a page by a content editor.  The use case is this: you want the same content delivered to different channels and formatted differently based on some parameter or session variable or device type, etc.

For example, if you want to render the content differently for web users vs. users getting the content via a mobile app, you can use the same content objects and just spit them out as json based on a parameter.  Below is code that will check to see if channel=mobile has been passed in as a parameter and will build the list of content objects that make up the page.  The $cons that is three lines from the bottom is a list of all the content on the page which can then be looped over to generate exactly what each channel would require.

This code should be placed at the top of your template, before any other code is executed:


#if("mobile" == $request.getParameter("channel"))
	#set($cons = $contents.getEmptyList())
	#foreach($vel in $context.getKeys())
			#set($x =$context.get("$vel"))
			#foreach($id in $x)
				#set($con = $dotcontent.find($id))
					#set($dummy = $cons.add($con))