Version 5.2 by Vincent Massol on 2017/09/05

Hide last authors
Manuel Smeria 3.1 1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
jeanvivienmaurice 1.11 4
Vincent Massol 5.1 5 Here are several solutions to include a page containing Velocity script into another page.
jeanvivienmaurice 1.1 6
Vincent Massol 5.1 7 = Use the ~{~{include}} Macro =
8
9 That's the recommended way when using XWiki Syntax 2.0+. See the [[Include Macro>>doc:extensions:Extension.Include Macro]].
10
11 Example:
12
13 {{code}}
14 {{include reference="some.page.containing.velocity"/}}
15 {{/code}}
16
Manuel Smeria 3.1 17 = Include a *.vm script =
jeanvivienmaurice 1.1 18
zzeroo 4.1 19 These macros take as parameter a relative URL to a {{code}}*.vm{{/code}} script.
jeanvivienmaurice 1.17 20
Manuel Smeria 3.1 21 == template() ==
jeanvivienmaurice 1.17 22
Manuel Smeria 3.1 23 {{info}}
24 Specification: [[extensions:Extension.Template Macro]]
25 {{/info}}
jeanvivienmaurice 1.1 26
Manuel Smeria 3.1 27 This is the equivalent of the C preprocessor include directive: the result is a copy of the script passed as argument, into your script.
jeanvivienmaurice 1.17 28
Vincent Massol 5.1 29 = Using the old #include* Velocity macros =
jeanvivienmaurice 1.17 30
Vincent Massol 5.1 31 These macros take as parameter the XWiki name of the page, ##Space.Page##.
Manuel Smeria 2.1 32
Manuel Smeria 3.1 33 == includeInContext() ==
jeanvivienmaurice 1.13 34
Manuel Smeria 3.1 35 {{info}}
36 Specification: [[extensions:Extension.Include In Context Macro]]
37 {{/info}}
jeanvivienmaurice 1.1 38
Manuel Smeria 3.1 39 If you have a script stored as an XWiki page you can include its code into another script by using includeInContext:
Manuel Smeria 2.1 40
Manuel Smeria 3.1 41 {{code}}
42 #includeInContext("mySpace.myPage")
43 {{/code}}
Manuel Smeria 2.1 44
Manuel Smeria 3.1 45 == includeForm() ==
46
47 {{info}}
48 Specification: [[extensions:Extension.Include Form Macro]]
49 {{/info}}
50
51 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:
52
Manuel Smeria 2.1 53 {{code language="velocity"}}
jeanvivienmaurice 1.1 54 ## Change class name to your class name
55 #set($class = $doc.getObject("CompanionTemplateSystem.CompanionDocumentMetaDataClass").xWikiClass)
56
57 <dl>
58 #foreach($prop in $class.properties)
59 #if(($prop.getName() != "Copyright") && ($prop.getName() != "TargetGroup") )
60 <dt> *${prop.prettyName}* </dt>
61 <dd>$doc.display($prop.getName())</dd>
62 #end
63 #end
64 </dl>
Manuel Smeria 2.1 65 {{/code}}
66
Manuel Smeria 3.1 67 which is stored as a regular XWiki page (let's say myPage in mySpace).
jeanvivienmaurice 1.1 68
Manuel Smeria 3.1 69 You will include the page using includeForm():
Manuel Smeria 2.1 70
Manuel Smeria 3.1 71 {{code}}
72 #includeForm("mySpace.myPage")
73 {{/code}}
jeanvivienmaurice 1.1 74
Manuel Smeria 3.1 75 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.
jeanvivienmaurice 1.14 76
Manuel Smeria 3.1 77 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 your user type is advanced.
78
79 == includeTopic() ==
80
81 {{info}}
82 Specification: [[extensions:Extension.Include Topic Macro]]
83 {{/info}}
84
85 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 be displayed properly.

Get Connected