How to include a velocity page into another page?

Last modified by Vincent Massol on 2020/01/28

Here are several solutions to include a page containing Velocity script into another page.

Use the {{include}} Macro

That's the recommended way when using XWiki Syntax 2.0+. See the Include Macro.

Example:

{{include reference="some.page.containing.velocity"/}}

Include a *.vm script

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

template()

Specification: Template Macro

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

Using the old #include* Velocity macros

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

includeInContext()

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

#includeInContext("mySpace.myPage")

includeForm()

Specification: Include Form Macro

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)

<dl>
#foreach($prop in $class.properties)
#if(($prop.getName() != "Copyright") && ($prop.getName() != "TargetGroup") )
<dt> *${prop.prettyName}* </dt>
<dd>$doc.display($prop.getName())</dd>
#end
#end
</dl>

which is stored as a regular XWiki page (let's say myPage in mySpace).

You will include the page using includeForm():

#includeForm("mySpace.myPage")

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 XWiki.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 your user type is advanced.

includeTopic()

Specification: Include Topic Macro

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 be displayed properly.

Get Connected