Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}{{toc/}}{{/box}}
2 XWiki integrates both Velocity and Groovy scripting. Together, these two mechanisms allow you to create basic to complex web applications at the XWiki page (or view) layer without the need for compiling code or deploying software components. In other words, you can use Velocity and Groovy script syntax in addition to wiki and HTML syntax as the contents of an XWiki page.
4 {{id name=velocity /}}
5 = XWiki's Velocity API
7 The concept of the 'context' is central to Velocity. The context is a 'carrier' of data between the Java layer of the XWiki engine and the template or page layer. The programmers of the XWiki core have gathered objects of various types and placed them in the Velocity context. These objects, and their methods and properties, are accessible via template elements called references and effectively form an API for XWiki.
9 The API is documented in Javadoc format and can be accessed here: [[XWiki API Javadoc>>DevGuide.API]]. If you are not familiar with Java or object oriented programming, you will probably be confused by the API documentation. It is not within the scope of our documentation to teach you all the details about Velocity, Java, or object oriented programming. You can find all of that information already online. You should then refer to the [[Velocity User Guide>>]] as an ongoing reference. Finally, you can explore the page code found throughout the [[Code Zone>>code:Main.WebHome]] area to see how others have figured out how to achieve a variety of results.
11 Velocity scripts can access the following:
12 * [[The current document>>]]: **##$doc##**
13 * [[The Context of the request>>]]: **##$context##**
14 * [[The Request object>>]]: **##$request##**
15 * [[The Response object>>]]: **##$response##**
16 * [[The XWiki object>>]]: **##$xwiki##**
18 In addition the following Velocity tools are also available in the Velocity context:
19 * [[List Tool>>]]: **##$listtool##**
20 * [[Number Tool>>]]: **##$numbertool##**
21 * [[Date Tool>>]]: **##$datetool##**
22 * [[Math Tool>>]]: **##$mathtool##**
23 * [[Escape Tool>>]]: **##$escapetool##**
24 * [[Sort Tool>>]]: **##$sorttool##**
26 {{info}}If you wish to add new Velocity tools you'll need to edit your file and follow the instructions in there.{{/info}}
28 You can also [[use HQL to query the XWiki database>>velocityHqlExamples]] from your velocity scripts.
30 To include Velocity scripts in other Velocity scripts, see [[How to include a velocity page into another page>>DevGuide.IncludeInVelocity]].
32 == Other Velocity Variables
34 {{info}}These variables can be used but are subject to change in the future.{{/info}}
36 === Controlling whether to display Comments/History/Attachment/Information sections or not
38 It's possible to control whether to display these sections by setting some velocity variable to "no":
40 {{code}}
41 #set ($showcomments = "no")
42 #set ($showattachments = "no")
43 #set ($showhistory = "no")
44 #set ($showinformation = "no")
45 {{/code}}
47 To remove them all you can set:
49 {{code}}
50 #set($docextras = [])
51 {{/code}}
53 = XWiki's Groovy API
55 Currently Groovy is only allowed for admins of a wiki (or users having the 'programming' right).
57 * See Groovy examples in the [[Code Zone>>code:Main.WebHome]], more specifically in the [[Code Snippets area>>code:Snippets.WebHome]]
58 * [[Feeling Groovy>>]]
59 * [[MVC programming with Groovy templates>>]]
61 == Groovy Example
63 The following example demonstrates how to use a groovy script to interact with velocity code in your page. This example performs a DNS lookup from the velocity variable ##$hostname## and stores the result in the variable ##$address##.
65 Using XWiki Syntax 2.0:
66 Objects can be passed back and forth between scripting languages by storing them in commonly available objects. One such commonly available object which only lasts the length of the request is the context object, known as xcontext.
67 {{code}}
68 {{velocity}}
69 #set($hostname = "")
70 Host Name: $hostname
71 $xcontext.put("hostname", $hostname)
72 {{/velocity}}
73 {{groovy}}
74 import;
75 host = xcontext.get("hostname");
76 InetAddress addr = InetAddress.getByName(host);
77 String address = addr.getHostAddress();
78 xcontext.put("address", address);
79 {{/groovy}}
80 {{velocity}}
81 IP Address: $xcontext.get("address")
82 {{/velocity}}
83 {{/code}}
85 Using XWiki Syntax 1.0:
86 Because Groovy and Velocity code are parsed together, variables defined in Groovy can be used directly in velocity without storing in and retrieving from the context.
87 {{code}}
88 #set ($hostname = "")
89 Host Name: $hostname
90 <%
91 import;
92 vcontext = context.get("vcontext");
93 host = vcontext.get("hostname");
94 InetAddress addr = InetAddress.getByName(host);
95 String address = addr.getHostAddress();
96 %>
97 IP Address: $address
98 {{/code}}

Get Connected