Hide last authors
Caleb James DeLisle 16.1 1 {{box cssClass="floatinginfobox" title="**Contents**"}}{{toc/}}{{/box}}
Vincent Massol 1.1 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.
Caleb James DeLisle 16.2 4 {{id name=velocity /}}
5 = XWiki's Velocity API
Vincent Massol 1.1 6
Caleb James DeLisle 16.1 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.
Vincent Massol 1.1 8
Caleb James DeLisle 17.2 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>>http://velocity.apache.org/engine/releases/velocity-1.6.2/user-guide.html]] 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.
Vincent Massol 1.1 10
Caleb James DeLisle 17.3 11 Velocity scripts can access the following:
Caleb James DeLisle 17.5 12 * [[The current document>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/api/Document.html]]: **##$doc##**
13 * [[The Context of the request>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/api/Context.html]]: **##$context##**
14 * [[The Request object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/web/XWikiRequest.html]]: **##$request##**
15 * [[The Response object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/web/XWikiResponse.html]]: **##$response##**
16 * [[The XWiki object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/api/XWiki.html]]: **##$xwiki##**
Vincent Massol 1.1 17
Vincent Massol 9.1 18 In addition the following Velocity tools are also available in the Velocity context:
Caleb James DeLisle 16.1 19 * [[List Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/ListTool.html]]: **##$listtool##**
20 * [[Number Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/NumberTool.html]]: **##$numbertool##**
21 * [[Date Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/DateTool.html]]: **##$datetool##**
22 * [[Math Tool>>http://velocity.apache.org/tools/releases/1.4/generic/MathTool.html]]: **##$mathtool##**
23 * [[Escape Tool>>http://velocity.apache.org/tools/releases/1.4/generic/EscapeTool.html]]: **##$escapetool##**
24 * [[Sort Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/SortTool.html]]: **##$sorttool##**
Vincent Massol 9.1 25
Caleb James DeLisle 16.1 26 {{info}}If you wish to add new Velocity tools you'll need to edit your ##xwiki.properties## file and follow the instructions in there.{{/info}}
Vincent Massol 9.2 27
Caleb James DeLisle 16.1 28 You can also [[use HQL to query the XWiki database>>velocityHqlExamples]] from your velocity scripts.
Vincent Massol 1.1 29
Caleb James DeLisle 16.1 30 To include Velocity scripts in other Velocity scripts, see [[How to include a velocity page into another page>>DevGuide.IncludeInVelocity]].
jeanvivienmaurice 1.20 31
Caleb James DeLisle 16.2 32 == Other Velocity Variables
Vincent Massol 13.1 33
Caleb James DeLisle 16.1 34 {{info}}These variables can be used but are subject to change in the future.{{/info}}
Vincent Massol 13.1 35
Caleb James DeLisle 16.2 36 === Controlling whether to display Comments/History/Attachment/Information sections or not
Vincent Massol 13.1 37
38 It's possible to control whether to display these sections by setting some velocity variable to "no":
Caleb James DeLisle 16.1 40 {{code}}
Vincent Massol 13.1 41 #set ($showcomments = "no")
42 #set ($showattachments = "no")
43 #set ($showhistory = "no")
44 #set ($showinformation = "no")
Caleb James DeLisle 16.1 45 {{/code}}
Vincent Massol 13.1 46
47 To remove them all you can set:
Caleb James DeLisle 16.1 49 {{code}}
Vincent Massol 13.1 50 #set($docextras = [])
Caleb James DeLisle 16.1 51 {{/code}}
Vincent Massol 13.1 52
Caleb James DeLisle 16.2 53 = XWiki's Groovy API
Vincent Massol 1.1 54
55 Currently Groovy is only allowed for admins of a wiki (or users having the 'programming' right).
Caleb James DeLisle 16.1 57 * See Groovy examples in the [[Code Zone>>code:Main.WebHome]], more specifically in the [[Code Snippets area>>code:Snippets.WebHome]]
58 * [[Feeling Groovy>>http://www-128.ibm.com/developerworks/java/library/j-alj08034.html]]
59 * [[MVC programming with Groovy templates>>http://www-128.ibm.com/developerworks/java/library/j-pg02155/]]
Vincent Massol 1.1 60
Caleb James DeLisle 16.2 61 == Groovy Example
gfiorentino 1.17 62
Caleb James DeLisle 18.1 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##.
Vincent Massol 8.2 64
Caleb James DeLisle 18.1 65 Using XWiki Syntax 1.0:
Caleb James DeLisle 16.1 66 {{code}}
Caleb James DeLisle 18.1 67 #set ($hostname = "www.xwiki.org")
68 Host Name: $hostname
Vincent Massol 8.2 69 <%
70 import java.net.InetAddress;
71 vcontext = context.get("vcontext");
Caleb James DeLisle 18.1 72 host = vcontext.get("hostname");
73 InetAddress addr = InetAddress.getByName(host);
74 String address = addr.getHostAddress();
Vincent Massol 8.2 75 %>
Caleb James DeLisle 18.1 76 IP Address: $address
Caleb James DeLisle 16.1 77 {{/code}}

Get Connected