How to include a velocity page into another page ?

Version 2.2 by Manuel Smeria on 2013/01/24

You have to use Velocity macros. Here are the possible macros you can use to this purpose :

Invalid macro parameters used for the [toc] macro. Cause: [Failed to validate bean: [must be greater than or equal to 1]]. Click on this message for details.

Include an *.vm script

These macros take as parameter a relative URL to a *.vm script.


Specification : TemplateMacro This is the equivalent of the C preprocessor include directive : the result is a copy of the script passed as argument, into your script.

Include the Velocity code of another XWiki page :

These macros take as parameter the XWiki name of a page, . .


Specification : IncludeInContextMacro If you have a script stored as an XWiki page, you can include its code into another script by using includeInContext :


Specification : IncludeFormMacro Similar to includeInContext(), except that using includeForm() in a page will set the default edit mode for that page as "inline". For example, the script you want to include uses the method com.xpn.xwiki.doc.XWikiDocument.display() to access a field of an object attached to the including page, like a typical class sheet :
## Change class name to your class name 
#set($class = $doc.getObject("CompanionTemplateSystem.CompanionDocumentMetaDataClass").xWikiClass)

#foreach($prop in $
#if(($prop.getName() != "Copyright") && ($prop.getName() != "TargetGroup") )
<dt> *${prop.prettyName}* </dt>
which is stored as a regular XWiki page (let's say myPage in mySpace).

You will include the page using includeForm() :

This way, the including page will always be edited in inline mode by default. Thus in the example of the class sheet, the including page will display input fields mapped to the respective object fields.

If you prefer to keep the default edit as set in your XWikiPreferences class, you can use the other macros, and still edit a page in inline mode : choose the "Inline form" option in the "Edit" menu, provided you have enabled "Advanced Edit" in your user profile.


Specification : IncludeTopicMacro Same syntax as includeInContext(). Contrary to includeInContext(), the included XWiki page is interpreted in its own context. For example, you would include a page using includeTopic() if the included page had to access its own objects in order for the including page to display properly.

Get Connected