How to Write a basic Velocity Script

Using 11.10.11. MySQL

I have been going through the scripting documentation page


and

I could not figure out a way to run a velocity script.

Where to keep the script and how to run it. Doc says the it is available by default.

{{velocity}}
Your username is $xcontext.getUser(), welcome to the site.
{{/velocity}}

I dont know where to start. This is completely new to me. Any pointers?

Best

Dominic

Your code works. Use source mode, or check permissions.

I was not aware and I could not get this information else where as well. Thank you.

For someone searching like me :slight_smile:

  1. Crete a new Page.

  2. Select Source

  3. Paste the following
    {{velocity}}
    #set ($mydoc = $xwiki.getDocument(“Operations.Docs.41293”))
    #set ($discard = $mydoc.setContent(“Sample Text.”))
    #set ($discard = $mydoc.save())
    #set ($mydoc = $xwiki.getDocument(“Operations.Docs.41294”))
    #set ($discard = $mydoc.setContent(“Sample Text.”))
    #set ($discard = $mydoc.save())
    {{/velocity}}

  4. Again press Source.

  5. Two documents will be created.

1 Like

You forgot to save… BTW the switch from source will execute the content by rendering the page and thus when you save you’ll also execute the script another time. You need to add protection if you don’t want to re-run it every time the page is viewed… :wink:

Also note that this is just one way. You can also edit using the wiki editor or use the WYSIWYG editor and insert macro then select Velocity.

I’d like to understand this part. You said you read https://www.xwiki.org/xwiki/bin/view/Documentation/DevGuide/Scripting/ but the first 2 paragraphs say:

Scripting allows 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 scripting syntax in addition to wiki and HTML syntax as the contents of an XWiki page.

Could you help me phrase it differently and in a way that you’d have understood?

FYI I’ve just modified the page and it now read:

Scripting allows 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 scripting syntax in addition to wiki markup inside the content of an XWiki page (e.g. see the script syntax for the XWiki 2.1 syntax).

Is that enough?

Note that if your issue was to find how to edit a page then the doc is at https://www.xwiki.org/xwiki/bin/view/Documentation/UserGuide/Features/PageEditing

Thanks for helping us improve the doc!