The [toc] macro is a standalone macro and it cannot be used inline
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. 

XWiki's Velocity API

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.

The API is documented in Javadoc format and can be accessed here: XWiki API Javadoc. 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 area to see how others have figured out how to achieve a variety of results.

Velocity scripts can access the following:

In addition the following Velocity tools are also available in the Velocity context:

If you wish to add new Velocity tools you'll need to edit your xwiki.properties file and follow the instructions in there.

You can also use HQL to query the XWiki database from your velocity scripts.

To include Velocity scripts in other Velocity scripts, see How to include a velocity page into another page.

Other Velocity Variables

These variables can be used but are subject to change in the future.

Controlling whether to display Comments/History/Attachment/Information sections or not

It's possible to control whether to display these sections by setting some velocity variable to "no":

#set ($showcomments = "no")
#set ($showattachments = "no")
#set ($showhistory = "no")
#set ($showinformation = "no")

To remove them all you can set:

#set($docextras = [])

XWiki's Groovy API

Currently Groovy is only allowed for admins of a wiki (or users having the 'programming' right).

Groovy Example

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.

Using XWiki Syntax 2.0:
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.
{{velocity}}
#set($hostname = "www.xwiki.org")
Host Name: $hostname
$xcontext.put("hostname", $hostname)
{{/velocity}}
{{groovy}}
import java.net.InetAddress;
host = xcontext.get("hostname");
InetAddress addr = InetAddress.getByName(host);
String address = addr.getHostAddress();
xcontext.put("address", address);
{{/groovy}}
{{velocity}}
IP Address: $xcontext.get("address")
{{/velocity}}

Using XWiki Syntax 1.0:
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.
#set ($hostname = "www.xwiki.org")
Host Name: $hostname
<%
import java.net.InetAddress;
vcontext = context.get("vcontext");
host = vcontext.get("hostname");
InetAddress addr = InetAddress.getByName(host);
String address = addr.getHostAddress();
%>
IP Address: $address

Tags:
Created by Vincent Massol on 2017/09/05
   

Get Connected