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