Changes for page Scripting
Last modified by Simon Urli on 2023/10/10
From version 15.1
edited by Raluca Stavro
on 2009/10/26
on 2009/10/26
Change comment:
There is no comment for this version
Summary
-
Page properties (3 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. RalucaMorosan1 +XWiki.CalebJamesDeLisle - Syntax
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki 1.01 +XWiki 2.0 - Content
-
... ... @@ -1,91 +1,80 @@ 1 -#startfloatingbox() 2 -*Contents* 3 -#toc ("2" "4" "") 4 -#endfloatingbox() 1 +{{box cssClass="floatinginfobox" title="**Contents**"}}{{toc/}}{{/box}} 5 5 6 - 1Scripting3 += Scripting 7 7 8 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 9 10 -<a name="velocity"></a> 11 - 1.1XWiki's Velocity API7 +{{html}}<a name="velocity"></a><!-- This is for backward compatibility because other pages may still link to thisPage#velocity -->{{/html}} 8 +== XWiki's Velocity API 12 12 13 -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 10 +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. 14 14 15 -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. 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. 12 +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. 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. 16 16 17 17 You can access in your velocity script to: 18 -* The current document: * <tt>\$doc</tt>*19 -* The Context of the request: * <tt>\$context</tt>*20 -* the request object: * <tt>\$request</tt>*21 -* the response object: * <tt>\$response</tt>*22 -* the XWiki Object: * <tt>\$xwiki</tt>*15 +* The current document: **##$doc##** 16 +* The Context of the request: **##$context##** 17 +* the request object: **##$request##** 18 +* the response object: **##$response##** 19 +* the XWiki Object: **##$xwiki##** 23 23 24 24 In addition the following Velocity tools are also available in the Velocity context: 25 -* [List Tool>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/ListTool.html]: * <tt>\$listtool</tt>*26 -* [Number Tool>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/NumberTool.html]: * <tt>\$numbertool</tt>*27 -* [Date Tool>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/DateTool.html]: * <tt>\$datetool</tt>*28 -* [Math Tool>http://velocity.apache.org/tools/releases/1.4/generic/MathTool.html]: * <tt>\$mathtool</tt>*29 -* [Escape Tool>http://velocity.apache.org/tools/releases/1.4/generic/EscapeTool.html]: * <tt>\$escapetool</tt>*30 -* [Sort Tool>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/SortTool.html]: * <tt>\$sorttool</tt>*22 +* [[List Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/ListTool.html]]: **##$listtool##** 23 +* [[Number Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/NumberTool.html]]: **##$numbertool##** 24 +* [[Date Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/DateTool.html]]: **##$datetool##** 25 +* [[Math Tool>>http://velocity.apache.org/tools/releases/1.4/generic/MathTool.html]]: **##$mathtool##** 26 +* [[Escape Tool>>http://velocity.apache.org/tools/releases/1.4/generic/EscapeTool.html]]: **##$escapetool##** 27 +* [[Sort Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/SortTool.html]]: **##$sorttool##** 31 31 32 - #info("If you wish to add new Velocity tools you'll need to edit your<tt>xwiki.properties</tt>file and follow the instructions in there.")29 +{{info}}If you wish to add new Velocity tools you'll need to edit your ##xwiki.properties## file and follow the instructions in there.{{/info}} 33 33 34 -You can also [use HQL to query the XWiki database>velocityHqlExamples] from your velocity scripts. 31 +You can also [[use HQL to query the XWiki database>>velocityHqlExamples]] from your velocity scripts. 35 35 36 -To include Velocity scripts in other Velocity scripts, see [How to include a velocity page into another page>DevGuide.IncludeInVelocity]. 33 +To include Velocity scripts in other Velocity scripts, see [[How to include a velocity page into another page>>DevGuide.IncludeInVelocity]]. 37 37 38 - 1.1.1Other Velocity Variables35 +=== Other Velocity Variables 39 39 40 - #info("These variables can be used but are subject to change in the future.")37 +{{info}}These variables can be used but are subject to change in the future.{{/info}} 41 41 42 - 1.1.1.1Controlling whether to display Comments/History/Attachment/Information sections or not39 +==== Controlling whether to display Comments/History/Attachment/Information sections or not 43 43 44 44 It's possible to control whether to display these sections by setting some velocity variable to "no": 45 45 46 -{code :none}43 +{{code}} 47 47 #set ($showcomments = "no") 48 48 #set ($showattachments = "no") 49 49 #set ($showhistory = "no") 50 50 #set ($showinformation = "no") 51 -{code} 48 +{{/code}} 52 52 53 53 To remove them all you can set: 54 54 55 -{code :none}52 +{{code}} 56 56 #set($docextras = []) 57 -{code} 54 +{{/code}} 58 58 59 - 1.1XWiki's Groovy API56 +== XWiki's Groovy API 60 60 61 61 Currently Groovy is only allowed for admins of a wiki (or users having the 'programming' right). 62 62 63 -* See Groovy examples in the [Code Zone>code:Main.WebHome], more specifically in the [Code Snippets area>code:Snippets.WebHome] 64 -* [Feeling Groovy>http://www-128.ibm.com/developerworks/java/library/j-alj08034.html] 65 -* [MVC programming with Groovy templates>http://www-128.ibm.com/developerworks/java/library/j-pg02155/] 66 -* [Guillaume Laforge on Groovy, XWiki etc.>http://www.stelligent.com/content/view/44/71/] 60 +* See Groovy examples in the [[Code Zone>>code:Main.WebHome]], more specifically in the [[Code Snippets area>>code:Snippets.WebHome]] 61 +* [[Feeling Groovy>>http://www-128.ibm.com/developerworks/java/library/j-alj08034.html]] 62 +* [[MVC programming with Groovy templates>>http://www-128.ibm.com/developerworks/java/library/j-pg02155/]] 63 +* [[Guillaume Laforge on Groovy, XWiki etc.>>http://www.stelligent.com/content/view/44/71/]] 67 67 68 - 1.1.1Groovy Example65 +=== Groovy Example 69 69 70 -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>.67 +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##. 71 71 72 -{code} 69 +{{code}} 73 73 #set ($address = "172.20.12.61") 74 - 75 75 IP Address: $address 76 - 77 77 <% 78 - 79 79 import java.net.InetAddress; 80 - 81 81 vcontext = context.get("vcontext"); 82 82 hostname = vcontext.get("address"); 83 - 84 84 InetAddress addr = InetAddress.getByName(hostname); 85 85 hostname = addr.getHostName().toLowerCase(); 86 - 87 87 %> 88 - 89 89 Hostname: $hostname 90 -{code} 91 - 80 +{{/code}}