Version 23.2 by Vincent Massol on 2010/12/13 13:51

cogExecutes a script implementing the [[JSR-223 API>>http://scripting.dev.java.net/]]
Typejavamacro
Category
Developed byUnknown
Rating
0 Votes
LicenseUnknown

Description

Executes a script implementing the JSR-223 API.

Usage

{{script language="scriptengine"}}
print "some" + " " + "script" + " " + "content"
{{/script}}

By default the "xwiki" and "xcontext" variables are defined and respectively represent a XWiki and Context API objects.

Parameters definition

NameOptionalAllowed valuesDefault valueDescriptionSince version
languageyesthe identifier of the JSR 223 engine Indicate which engine to use to execute the provided script 
outputyestrue/falsetrueIndicate the output result has to be inserted back in the document1.8.1, 1.9M1
wikiyestrue/falsetrueindicate if the result of the script execution has to be parsed by the current wiki parser. If not it's put in a verbatim block.2.0 M1
jarsyescomma-separated list of JARs that will be added to the script execution class loadernonesee below2.0RC1

Specifying extra JARs

If you have programming rights, it's possible to add JARs that will be available to the script's execution by using the jars parameters. The format is a comma-separated list of entries of the following types:

Bindings

Some bindings/variable are automatically provided: 

NameClassDescription
xwikicom.xpn.xwiki.api.XWikiUtility APIs
xcontextcom.xpn.xwiki.api.ContextContains contextual informations
requestcom.xpn.xwiki.web.XWikiRequestThe servlet request. Generaly used to get URL parameters.
responsecom.xpn.xwiki.web.XWikiResponseThe servlet response
doccom.xpn.xwiki.api.DocumentThe current document
utilcom.xpn.xwiki.api.UtilUtility APIs
msgcom.xpn.xwiki.web.XWikiMessageTooll10n tool
syntaxFactoryorg.xwiki.rendering.syntax.SyntaxFactorygenerate proper Syntax object from syntax identifier

Return

Since 2.4M2 it's possible to directly return the result of the macro execution as a single Block, a list of Block or a XDOM. That allow to cover some use case where you already have the block (you got from a parsing for example) and want to insert them into the document or when you want to do safest escaping possible.

The way to return a value from a script totally depends on the language and the engine used. Note that this is available only for JSR 223 based macro for now which mean pretty much all macros except velocity one.

Here is an example with groovy macro:

{{groovy}}
return java.util.Arrays.asList(new org.xwiki.rendering.block.WordBlock("Hello"), org.xwiki.rendering.block.SpaceBlock.SPACE_BLOCK, new org.xwiki.rendering.block.WordBlock("world"));
{{/groovy}}

Nested scripts

Since 2.4M2 it is no longer possible to directly nest script macros inside each other. For example, this would NOT work any more:

{{velocity}}
#set($data = "test")

## nested groovy, forbidden!
{{groovy}}
println("$data");
{{/groovy}}

{{/velocity}}

Nested scripts present a serious security risk, because they can be used to bypass security checks. We strongly advise to write your scripts so that the data is passed through variables. If you really need the old behavior, you can remove NestedScriptMacroValidator from components.txt in xwiki-rendering-macro-script component.

Example

{{script language=groovy}}
def list = ["one", "two"]
list.each { item ->
  println "* ${item}"
}
{{/script}}

Result

  • one
  • two

Get Connected