Wiki source code of Scripting

Version 10.1 by TharinduMadushanka on 2009/04/14

Hide last authors
Vincent Massol 8.3 1 #startfloatingbox()
2 *Contents*
3 #toc ("2" "3" "")
4 #endfloatingbox()
5
Vincent Massol 1.1 6 1 Scripting
7
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.
9
10 1.1 XWiki's Velocity API
11
TharinduMadushanka 10.1 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 accessible via template elements called references and effectively form an API for XWiki.
Vincent Massol 1.1 13
Vincent Massol 8.1 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>http://velocity.apache.org/engine/releases/velocity-1.5/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 15
Vincent Massol 9.1 16 You can access in your velocity script to:
Vincent Massol 1.1 17
TharinduMadushanka 10.1 18 * The current document: *\$doc*
19 * The Context of the request: *\$context*
20 * the request object: *\$request*
21 * the response object: *\$response*
22 * the XWiki Object: *\$xwiki*
23
Vincent Massol 9.1 24 In addition the following Velocity tools are also available in the Velocity context:
25
TharinduMadushanka 10.1 26 * [List Tool>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/ListTool.html]: *\$listtool*
27 * [Number Tool>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/NumberTool.html]: *\$numbertool*
28 * [Date Tool>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/DateTool.html]: *\$datetool*
29 * [Math Tool>http://velocity.apache.org/tools/releases/1.4/generic/MathTool.html]: *\$mathtool*
30 * [Escape Tool>http://velocity.apache.org/tools/releases/1.4/generic/EscapeTool.html]: *\$escapetool*
31 * [Sort Tool>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/SortTool.html]: *\$sorttool*
Vincent Massol 9.2 32
TharinduMadushanka 10.1 33 #info("If you wish to add new Velocity tools you'll need to edit your WEB-INF/plexus.xml file and copy the full component definition for the org.xwiki.velocity.VelocityContextFactory role from this [Plexus Component Definition file>http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-velocity/src/main/resources/META-INF/plexus/components.xml]")
34
Vincent Massol 1.4 35 You can also [use HQL to query the XWiki database>velocityHqlExamples] from your velocity scripts.
Vincent Massol 1.1 36
Vincent Massol 5.1 37 To include Velocity scripts in other Velocity scripts, see [How to include a velocity page into another page>DevGuide.IncludeInVelocity].
jeanvivienmaurice 1.20 38
Vincent Massol 1.5 39 1.1 XWiki's Groovy API
Vincent Massol 1.1 40
41 Currently Groovy is only allowed for admins of a wiki (or users having the 'programming' right).
42
Vincent Massol 4.1 43 * See Groovy examples in the [Code Zone>code:Main.WebHome], more specifically in the [Code Snippets area>code:Snippets.WebHome]
Vincent Massol 1.1 44 * [Feeling Groovy>http://www-128.ibm.com/developerworks/java/library/j-alj08034.html]
45 * [MVC programming with Groovy templates>http://www-128.ibm.com/developerworks/java/library/j-pg02155/]
Vincent Massol 1.16 46 * [Guillaume Laforge on Groovy, XWiki etc.>http://www.stelligent.com/content/view/44/71/]
Vincent Massol 1.1 47
Vincent Massol 8.2 48 1.1.1 Groovy Example
gfiorentino 1.17 49
TharinduMadushanka 10.1 50 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 \$address and store the result into the variable \$hostname.
51 \\
Vincent Massol 8.2 52
53 {code}
54 #set ($address = "172.20.12.61")
55
56 IP Address: $address
57
58 <%
59
60 import java.net.InetAddress;
61
62 vcontext = context.get("vcontext");
63 hostname = vcontext.get("address");
64
65 InetAddress addr = InetAddress.getByName(hostname);
66 hostname = addr.getHostName().toLowerCase();
67
68 %>
69
70 Hostname: $hostname
TharinduMadushanka 10.1 71
Vincent Massol 8.2 72 {code}
73
TharinduMadushanka 10.1 74 \\
75
76

Get Connected