Wiki source code of IncludeInVelocity
Version 1.22 by Vincent Massol on 2007/12/10
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | 1 How to include a velocity page into another page ? | ||
2 | |||
3 | You have to use Velocity macros. Here are the possible macros you can use to this purpose : | ||
4 | |||
5 | #toc("" "" "") | ||
6 | |||
7 | 1.1 Include an <code>\*.vm</code> script | ||
8 | |||
9 | These macros take as parameter a relative URL to a <code>*.vm</code> script. | ||
10 | |||
11 | 1.1.1 <code>template()</code> | ||
12 | |||
13 | #info("Specification : [code:Macros.TemplateMacro]") | ||
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 | |||
16 | 1.1 Include the Velocity code of another XWiki page : | ||
17 | |||
18 | These macros take as parameter the XWiki name of a page, \<Web\>.\<Page\> . | ||
19 | |||
20 | 1.1.1 <code>includeInContext()</code> | ||
21 | #info("Specification : [code:Macros.IncludeInContextMacro]") | ||
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 | |||
27 | 1.1.1 <code>includeForm()</code> | ||
28 | |||
29 | #info("Specification : [code:Macros.IncludeFormMacro]") | ||
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". | ||
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 : | ||
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} | ||
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> : | ||
48 | {code} | ||
49 | #includeForm("mySpace.myPage") | ||
50 | {code} | ||
51 | |||
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. | ||
53 | |||
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. | ||
55 | |||
56 | |||
57 | 1.1.1 <code>includeTopic()</code> | ||
58 | #info("Specification : [code:Macros.IncludeTopicMacro]") | ||
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. |