Show last authors
1 #startfloatingbox()
2 *Contents*
3 #toc ("2" "3" "")
4 #endfloatingbox()
6 1 Scripting
8 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.
10 1.1 XWiki's Velocity API
12 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 accessable via template elements called references and effectively form an API for XWiki.
14 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. Instead, we provide only a high-level overview in [Basic Velocity Scripting Tutorial>Basic Velocity]; it will give you a good sense for how Velocity scripting works in XWiki. 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.
16 You can access in your velocity script to:
17 * The current document: *<tt>\$doc</tt>*
18 * The Context of the request: *<tt>\$context</tt>*
19 * the request object: *<tt>\$request</tt>*
20 * the response object: *<tt>\$response</tt>*
21 * the XWiki Object: *<tt>\$xwiki</tt>*
23 In addition the following Velocity tools are also available in the Velocity context:
24 * [List Tool>]: *<tt>\$listtool</tt>*
25 * [Number Tool>]: *<tt>\$numbertool</tt>*
26 * [Date Tool>]: *<tt>\$datetool</tt>*
27 * [Math Tool>]: *<tt>\$mathtool</tt>*
28 * [Escape Tool>]: *<tt>\$escapetool</tt>*
29 * [Sort Tool>]: *<tt>\$sorttool</tt>*
31 #info("If you wish to add new Velocity tools you'll need to edit your <tt>WEB-INF/plexus.xml</tt> file and copy the full component definition for the <tt>org.xwiki.velocity.VelocityContextFactory</tt> role from this [Plexus Component Definition file>]")
33 You can also [use HQL to query the XWiki database>velocityHqlExamples] from your velocity scripts.
35 To include Velocity scripts in other Velocity scripts, see [How to include a velocity page into another page>DevGuide.IncludeInVelocity].
37 1.1 XWiki's Groovy API
39 Currently Groovy is only allowed for admins of a wiki (or users having the 'programming' right).
41 * See Groovy examples in the [Code Zone>code:Main.WebHome], more specifically in the [Code Snippets area>code:Snippets.WebHome]
42 * [Feeling Groovy>]
43 * [MVC programming with Groovy templates>]
44 * [Guillaume Laforge on Groovy, XWiki etc.>]
46 1.1.1 Groovy Example
48 The following example demonstrates how to use a groovy script directly inside your page. This example performs a reverse DNS lookup from the velocity variable <tt>\$address</tt> and store the result into the variable <tt>\$hostname</tt>.
50 {code}
51 #set ($address = "")
53 IP Address: $address
55 <%
57 import;
59 vcontext = context.get("vcontext");
60 hostname = vcontext.get("address");
62 InetAddress addr = InetAddress.getByName(hostname);
63 hostname = addr.getHostName().toLowerCase();
65 %>
67 Hostname: $hostname
68 {code}

Get Connected