How to include a velocity page into another page ?

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

Include an *.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.

Include the Velocity code of another XWiki page :

These macros take as parameter the XWiki name of a page, <Web>.<Page> .

includeInContext()

Specification : Include In Context Macro
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 Preferences 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.

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 display properly.
Version 1.22 last modified by VincentMassol on 10/12/2007 at 18:07

Comments 6

VincentMassol | 12.11.2007 at 06:27 PM
Jean-Vivien, you should reference the page describing these macros in the Code zone. Actually it would be nice to move all your content there and only link from here. Thanks!

jeanvivienmaurice | 13.11.2007 at 10:48 AM
Point taken.

jeanvivienmaurice | 13.11.2007 at 10:54 AM
Which section shall I put it in ? Snippet, Macro ? Thanks

VincentMassol | 13.11.2007 at 03:07 PM
There are already existing documentation for the includeContext and includeForm macros.

VincentMassol | 13.11.2007 at 03:10 PM
Forgot to mention: you're doing a great job! Thanks a lot for doing all of these improvements :) It really helps.

jeanvivienmaurice | 15.11.2007 at 08:29 AM
Shall I delete even more text in this page ? Thanks.

Attachments 0

No attachments for this document

Creator: jeanvivienmaurice on 2007/11/12 11:46
This wiki is licensed under a Creative Commons license
1.5.2.12758