Wiki source code of How to include a velocity page into another page ?
Version 2.2 by Manuel Smeria on 2013/01/24
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | You have to use Velocity macros. Here are the possible macros you can use to this purpose : | ||
2 | |||
3 | {{toc start="" depth="" numbered=""/}} | ||
4 | |||
5 | {{html clean="false" wiki="true"}} | ||
6 | = Include an <code>*.vm</code> script = | ||
7 | |||
8 | These macros take as parameter a relative URL to a <code>*.vm</code> script. | ||
9 | |||
10 | == <code>template()</code> == | ||
11 | |||
12 | {{info}}Specification : [[code:Macros.TemplateMacro]]{{/info}} | ||
13 | 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. | ||
14 | |||
15 | = Include the Velocity code of another XWiki page : = | ||
16 | |||
17 | These macros take as parameter the XWiki name of a page, <Web>.<Page> . | ||
18 | |||
19 | == <code>includeInContext()</code> == | ||
20 | |||
21 | {{info}}Specification : [[code:Macros.IncludeInContextMacro]]{{/info}} | ||
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 | |||
24 | {{code}}#includeInContext("mySpace.myPage"){{/code}} | ||
25 | |||
26 | == <code>includeForm()</code> == | ||
27 | |||
28 | {{info}}Specification : [[code:Macros.IncludeFormMacro]]{{/info}} | ||
29 | 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". | ||
30 | 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 : | ||
31 | |||
32 | {{code language="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 | |||
46 | which is stored as a regular XWiki page (let's say <code>myPage</code> in <code>mySpace</code>). | ||
47 | <p/> | ||
48 | You will include the page using <code>includeForm()</code> : | ||
49 | |||
50 | {{code}}#includeForm("mySpace.myPage"){{/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 | <p/> | ||
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 | == <code>includeTopic()</code> == | ||
57 | |||
58 | {{info}}Specification : [[code:Macros.IncludeTopicMacro]]{{/info}} | ||
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. | ||
60 | {{/html}} |