Wiki source code of IncludeInVelocity
Version 1.22 by Vincent Massol on 2007/12/10
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
1.1 | 1 | 1 How to include a velocity page into another page ? |
2 | |||
![]() |
1.11 | 3 | You have to use Velocity macros. Here are the possible macros you can use to this purpose : |
4 | |||
![]() |
1.3 | 5 | #toc("" "" "") |
![]() |
1.1 | 6 | |
![]() |
1.18 | 7 | 1.1 Include an <code>\*.vm</code> script |
![]() |
1.1 | 8 | |
![]() |
1.17 | 9 | These macros take as parameter a relative URL to a <code>*.vm</code> script. |
10 | |||
11 | 1.1.1 <code>template()</code> | ||
12 | |||
![]() |
1.22 | 13 | #info("Specification : [code:Macros.TemplateMacro]") |
![]() |
1.1 | 14 | This is the equivalent of the C preprocessor <code>include</code> directive : the result is a copy of the script passed as argument, into your script. |
15 | |||
![]() |
1.17 | 16 | 1.1 Include the Velocity code of another XWiki page : |
17 | |||
![]() |
1.20 | 18 | These macros take as parameter the XWiki name of a page, \<Web\>.\<Page\> . |
![]() |
1.17 | 19 | |
20 | 1.1.1 <code>includeInContext()</code> | ||
![]() |
1.22 | 21 | #info("Specification : [code:Macros.IncludeInContextMacro]") |
![]() |
1.13 | 22 | If you have a script stored as an XWiki page, you can include its code into another script by using <code>includeInContext</code> : |
23 | {code} | ||
24 | #includeInContext("mySpace.myPage") | ||
25 | {code} | ||
26 | |||
![]() |
1.17 | 27 | 1.1.1 <code>includeForm()</code> |
![]() |
1.1 | 28 | |
![]() |
1.22 | 29 | #info("Specification : [code:Macros.IncludeFormMacro]") |
![]() |
1.14 | 30 | Similar to <code>includeInContext()</code>, except that using <code>includeForm()</code> in a page will set the default edit mode for that page as "inline". |
![]() |
1.15 | 31 | For example, the script you want to include uses the method <code>com.xpn.xwiki.doc.XWikiDocument.display()</code> to access a field of an object attached to the including page, like a typical class sheet : |
![]() |
1.1 | 32 | {code:velocity} |
33 | ## Change class name to your class name | ||
34 | #set($class = $doc.getObject("CompanionTemplateSystem.CompanionDocumentMetaDataClass").xWikiClass) | ||
35 | |||
36 | <dl> | ||
37 | #foreach($prop in $class.properties) | ||
38 | #if(($prop.getName() != "Copyright") && ($prop.getName() != "TargetGroup") ) | ||
39 | <dt> *${prop.prettyName}* </dt> | ||
40 | <dd>$doc.display($prop.getName())</dd> | ||
41 | #end | ||
42 | #end | ||
43 | </dl> | ||
44 | {code} | ||
![]() |
1.15 | 45 | which is stored as a regular XWiki page (let's say <code>myPage</code> in <code>mySpace</code>). |
46 | |||
47 | You will include the page using <code>includeForm()</code> : | ||
![]() |
1.1 | 48 | {code} |
49 | #includeForm("mySpace.myPage") | ||
50 | {code} | ||
51 | |||
![]() |
1.15 | 52 | This way, the including page will always be edited in <code>inline</code> mode by default. Thus in the example of the class sheet, the including page will display input fields mapped to the respective object fields. |
![]() |
1.1 | 53 | |
![]() |
1.16 | 54 | 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 you have enabled "Advanced Edit" in your user profile. |
![]() |
1.14 | 55 | |
![]() |
1.15 | 56 | |
![]() |
1.17 | 57 | 1.1.1 <code>includeTopic()</code> |
![]() |
1.22 | 58 | #info("Specification : [code:Macros.IncludeTopicMacro]") |
![]() |
1.13 | 59 | Same syntax as <code>includeInContext()</code>. Contrary to <code>includeInContext()</code>, the included XWiki page is interpreted in its own context. For example, you would include a page using <code>includeTopic()</code> if the included page had to access its own objects in order for the including page to display properly. |