Changes for page Scripting
Last modified by Mohammad Humayun Khan on 2021/07/22
Change comment:
There is no comment for this version
Summary
-
Page properties (2 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. VincentMassol1 +XWiki.ThomasMortagne - Content
-
... ... @@ -25,6 +25,7 @@ 25 25 * [[The Request object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/web/XWikiRequest.html]]: **##request##** 26 26 * [[The Response object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/web/XWikiResponse.html]]: **##response##** 27 27 * [[The XWiki object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/api/XWiki.html]]: **##xwiki##** 28 +* [[The XWiki utils>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/api/Util.html]]: **##util##** 28 28 29 29 == [[XWiki Component>>extensions:Extension.Component Module]] Access == 30 30 ... ... @@ -32,20 +32,24 @@ 32 32 Also see: [[Accessing components from Groovy>>DevGuide.WritingComponents#HAccessingacomponentfromgroovy]] 33 33 Note: This snippet is written in Groovy and will have to be converted to your scripting language. 34 34 35 -{{code language="java"}}{{groovy}} 36 +{{code language="java"}} 37 +{{groovy}} 36 36 def greeter = com.xpn.xwiki.web.Utils.getComponent(org.xwiki.component.HelloWorld.class); 37 37 println greeter.sayHello(); 38 -{{/groovy}}{{/code}} 40 +{{/groovy}} 41 +{{/code}} 39 39 40 40 == XWiki Core Access == 41 41 42 42 Sometimes the XWiki Api doesn't provide the methods which you need for your application. you can gain raw access the core of XWiki but it presents an increased security risk and requires programming rights to run. Using the core should be avoided if at all possible. 43 43 44 -{{code language="java"}}{{groovy}} 47 +{{code language="java"}} 48 +{{groovy}} 45 45 def xc = xcontext.getContext(); 46 46 def wiki = xc.getWiki(); 47 47 def xdoc = doc.getDocument(); 48 -{{/groovy}}{{/code}} 52 +{{/groovy}} 53 +{{/code}} 49 49 50 50 After using this snippet, you will have 3 new objects: 51 51 ... ... @@ -137,12 +137,14 @@ 137 137 138 138 Example: 139 139 140 -{{code}}{{velocity}} 145 +{{code}} 146 +{{velocity}} 141 141 #if ($hasAdmin) 142 142 ## This link will only be visible to users that have admin rights on this document 143 143 [[Do some admin action>>Some.Document]] 144 144 #end 145 -{{/velocity}}{{/code}} 151 +{{/velocity}} 152 +{{/code}} 146 146 147 147 148 148 === Information about the current wiki === ... ... @@ -168,7 +168,8 @@ 168 168 Using XWiki Syntax 2.0: 169 169 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. 170 170 171 -{{code}}{{velocity}} 178 +{{code}} 179 +{{velocity}} 172 172 #set($hostname = "www.xwiki.org") 173 173 Host Name: $hostname 174 174 $xcontext.put("hostname", $hostname) ... ... @@ -182,12 +182,14 @@ 182 182 {{/groovy}} 183 183 {{velocity}} 184 184 IP Address: $xcontext.get("address") 185 -{{/velocity}}{{/code}} 193 +{{/velocity}} 194 +{{/code}} 186 186 187 187 Using XWiki Syntax 1.0: 188 188 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. 189 189 190 -{{code}}#set ($hostname = "www.xwiki.org") 199 +{{code}} 200 +#set ($hostname = "www.xwiki.org") 191 191 Host Name: $hostname 192 192 <% 193 193 import java.net.InetAddress; ... ... @@ -196,15 +196,18 @@ 196 196 InetAddress addr = InetAddress.getByName(host); 197 197 String address = addr.getHostAddress(); 198 198 %> 199 -IP Address: $address{{/code}} 209 +IP Address: $address 210 +{{/code}} 200 200 201 201 = Python Specific Information = 202 202 203 203 You can run python code in XWiki just like velocity or groovy. 204 204 205 -{{code language="python"}}{{python}} 216 +{{code language="python"}} 217 +{{python}} 206 206 print "The full name of this document is " + doc.getFullName() 207 -{{/python}}{{/code}} 219 +{{/python}} 220 +{{/code}} 208 208 209 209 {{warning}} 210 210 Versions prior to [[XWiki Enterprise 2.4>>xwiki:ReleaseNotes.ReleaseNotesXWikiEnterprise24]] have a bug which prevents you from having access to the default objects (doc, xcontext, request, etc.) a [[workaround is available in the Extensions wiki>>extensions:Extension.Access To Bindings In Python]]