Changes for page Scripting
Last modified by Mohammad Humayun Khan on 2021/07/22
Change comment:
Add a Syntax 2.0 port of the Groovy example.
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -62,7 +62,28 @@ 62 62 63 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##. 64 64 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 = "www.xwiki.org") 70 +Host Name: $hostname 71 +$xcontext.put("hostname", $hostname) 72 +{{/velocity}} 73 +{{groovy}} 74 +import java.net.InetAddress; 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}} 84 + 65 65 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. 66 66 {{code}} 67 67 #set ($hostname = "www.xwiki.org") 68 68 Host Name: $hostname