[opencms-dev] On how to integrate OpenCMS and velocity:

Werner Punz werpu at gmx.at
Thu Mar 28 19:27:43 CET 2002


Ok here is the additional info I promised:

I hope the connectors between OpenCMS and Velocity are already posted on the
OpenCMS site (haven´t had time to check that yet)
But here is a small example on how to integrate both.

The approach is rather straight forward:

First of all you just need a pretty empty html form:

<![CDATA[
put your standard OpenCMS HTML code here

]]><process>velocity_code</process>



Feel free to put the process tag wherever you like. All you now have to do
is, to connect an appropriate Velocity template
into the process tag. You can do it like that (I hope this code works ootb
since I copied it from splitted code in my own work framework:



		static final String VELOCITY_FIELD = "velocity_code";
		static final String VELOCITY_TEMPLATE = "theVelocityTemplate"; //you could
use an absolute path absolute from / here in this case the internal path is
searched

		public byte[] getContent(CmsObject cms, String templateFile, String
elementName, Hashtable theParameters, String templateSelector) throws
CmsException
	{

		CmsXmlTemplateFile templateDocument = getOwnTemplateFile(cms,
templateFile, elementName, theParameters, templateSelector);

		templateDocument.setData(VELOCITY_FIELD, " "); //this is necessary to
avoid an error in case the field is not set
		CmsSession session = (CmsSession)
cms.getRequestContext().getSession(true);


		/*
		 Do whatever you like here and then pass it down into the parameter list
		*/
		try
		{
			parseResult = (String)
LC_Velocity_OpenCMS.getInstance().parseTemplate(VELOCITY_TEMPLATE,
theParameters, cms);
		}
		catch (Exception ex)
		{
			//do error handling
		}
		if (parseResult!=null)
			templateDocument.setData(VELOCITY_FIELD, parseResult);

		return startProcessing(cms, templateDocument, elementName, theParameters,
templateFile);
	}



This is my current approach, there are two downsides, a publish won´t
publish a Velocity Template you have to publish yourself and you have to
take care about the correct user settings in the velocity template.

However the advantages in my opinion outweight the disadvantages by tons.
You pretty much can automate standard form passing this way (aka parameters
are passed from one from to the other and then are interpreted), as I also
said it is rather easy to iterate over collections, simply pass a vector of
the collection down the parameter list and do it from Velocity:

here is an example from a template in my current development system:

----

<center><H3>Following users were found</H3></center>
<br><br>
	#if($resultset)
		#foreach ($user in $resultset)
			<li>
			#if($user.getNewsletter())
				<A
HREF="JavaScript:getCurrUrl('user_list.html?operation=query&operation2=deakt
ivate&
				id=$user.getId()')">[User is active]</A>
			#else
				<A
HREF="JavaScript:getCurrUrl('user_list.html?operation=query&operation2=aktiv
ate&
				id=$user.getId()')">[User is inactive]</A>
			#end
				$user.getNachname() $user.getVorname() $user.getAdresse() $user.getPlz()
$user.getEmail() <A
HREF="JavaScript:getCurrUrl('user_edit.html?operation=query&id=$user.getId()
')">Edit user</A>
			<br>
		#end
	#else
		There were no entries found
	#end
<br><br>

---

You can see where this leads to:
a) You can iterate over collections
b) You can access bean like objects within your parameter list
($user.getPlz() for instance gives back the zip code as string of a user
entry)

and you can have generally a very dynamic content without to much coding
effort(have a look at the outer if statement in my example- it either gives
a list of users or an error message that no users were found, the inner if
can switch between an active and inactive user). And as well the whole
syntax is very close to PHP so lots of web designer shouldn´t have to many
problems recognizing the code and what it does.

As for integration into Ope
nCMS, I´m not really sure what the best approach is. One very good one would
be to have Velocity as additional type, another one would be to be able to
add a <velocity> entry into the page management file which could point to a
related Velocity file which then could be displayed optionally (very similar
to my approach-which tried to avoid to alter the OpenCMS system)-Actually I
would prefer both approaches or if not possible the second one since it
doesn´t break the current xml template structure.

I hope this sums up the handling of the connectors (additionally to the
readme file)

Happy coding (and feel free to contact me if there are questions)

Werner Punz Labor_C-Austria
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20020328/e5b5df62/attachment.htm>


More information about the opencms-dev mailing list