Wiki source code of Velocity Training

Version 47.1 by Lucas Charpentier (Sereza7) on 2025/04/04

Hide last authors
Ecaterina Moraru (Valica) 37.3 1 {{box cssClass="floatinginfobox" title="**Contents**"}}
Ecaterina Moraru (Valica) 37.6 2 {{toc depth="2" start="2"/}}
Ecaterina Moraru (Valica) 37.3 3 {{/box}}
4
Guillaume Delhumeau 1.1 5 = How to use Velocity in XWiki pages =
Vincent Massol 36.1 6
Guillaume Delhumeau 1.1 7 == What is it? ==
8
Sergei Kulagin 38.3 9 === ===
Guillaume Delhumeau 1.1 10
11 * Velocity is a **template** language.
12 * It describes what your page should be.
Simpel 38.4 13 ** Which means it **controls** what you **display**.
Guillaume Delhumeau 1.1 14
15 Examples:
Vincent Massol 36.1 16
Guillaume Delhumeau 1.1 17 * display the name of the user
18 * display an image under certain circonstances only
19 * display a list of all the blog categories
20
21 === Example ===
Vincent Massol 36.1 22
Guillaume Delhumeau 32.1 23 {{code language="velocity"}}
Pascal Bastien 34.1 24 {{velocity}}
Guillaume Delhumeau 1.1 25 Welcome $xcontext.user !
26 #if($hasAdmin)
27 You will see the following picture because you are an administrator:
28 image:picture.jpg
29 #end
Pascal Bastien 34.2 30 {{/velocity}}
Guillaume Delhumeau 32.1 31 {{/code}}
Guillaume Delhumeau 1.1 32
33 Will output:
Lucas Charpentier (Sereza7) 43.3 34 {{image reference="sample1.png" width="950px"/}}
Guillaume Delhumeau 1.1 35
Vincent Massol 36.1 36 === ===
37
Guillaume Delhumeau 1.1 38 * With XWiki and Velocity, you can retrieve informations contained in other pages
39
Guillaume Delhumeau 32.2 40 {{code language="velocity"}}
Guillaume Delhumeau 1.1 41 #set($docRef = $services.model.createDocumentReference('', 'OtherSpace', 'OtherPage'))
42 #set($document = $xwiki.getDocument($docRef))
43 The content of the other document is:
44 $document.getContent()
Guillaume Delhumeau 32.2 45 {{/code}}
Guillaume Delhumeau 1.1 46
47
Lucas Charpentier (Sereza7) 43.1 48 {{image reference="serrure.jpg" width="150px"/}}
Guillaume Delhumeau 1.1 49
Vincent Massol 36.1 50 === ===
Guillaume Delhumeau 1.1 51
52 * or do advanced searches throught the wiki (via the //Query Module//) :
53 ** //« give me all the blog entries posted during the month of july 2012 and that are not archived and that have at least 3 comments and... »//
54
Lucas Charpentier (Sereza7) 43.1 55 {{image reference="detective.jpg" width="350px"/}}
Guillaume Delhumeau 1.1 56
57 === ===
Vincent Massol 36.1 58
Guillaume Delhumeau 1.1 59 * But XWiki also allows you to use Velocity to **save** things in the wiki.
60
Guillaume Delhumeau 32.2 61 {{code language="velocity"}}
Guillaume Delhumeau 1.1 62 #set($object = $doc.getObject('XWiki.XWikiUsers'))
63 $object.set('first_name', 'Nobody')
64 $doc.save()
Guillaume Delhumeau 32.2 65 {{/code}}
Guillaume Delhumeau 1.1 66
67
Lucas Charpentier (Sereza7) 43.1 68 {{image reference="nobody.jpg" width="650px"/}}
Guillaume Delhumeau 1.1 69
70 == Principles ==
71
72 === Lines ===
Vincent Massol 36.1 73
Guillaume Delhumeau 1.1 74 * A line begining by '#' is a Velocity instruction
75 * All other lines are normal lines
76
Guillaume Delhumeau 32.2 77 {{code language="velocity"}}
Guillaume Delhumeau 1.1 78 ## This is a velocity comment - a comment is never displayed and is used to
79 ## add useful information inside a source code.
80 #set($myVar = 42)
81 ## The line above was a velocity instruction
82 This line is a normal line
Guillaume Delhumeau 32.2 83 {{/code}}
Guillaume Delhumeau 1.1 84
85 === Variables ===
Vincent Massol 36.1 86
Clemens Robbenhaar 38.1 87 * A //variable// is like a box that can contain data: text, numbers, objects, etc...
Guillaume Delhumeau 1.1 88 * A variable is always prefixed by a dollar '$'.
89 * To create a variable, just use the //#set()// Velocity instruction
90
Guillaume Delhumeau 32.2 91 {{code language="velocity"}}
Guillaume Delhumeau 1.1 92 #set($myVar = 42)
Guillaume Delhumeau 32.2 93 {{/code}}
Guillaume Delhumeau 1.1 94
95 * If a variable is inside a normal line, the content of the variable will be displayed:
96
Guillaume Delhumeau 32.2 97 {{code language="velocity"}}
Guillaume Delhumeau 1.1 98 #set($myVar = 42)
99 I am $myVar years old.
Guillaume Delhumeau 32.2 100 {{/code}}
Guillaume Delhumeau 1.1 101
102 will display:
103
Lucas Charpentier (Sereza7) 43.1 104 {{image reference="variables.png" width="350px"/}}
Guillaume Delhumeau 1.1 105
Vincent Massol 36.1 106 === ===
107
Guillaume Delhumeau 1.1 108 Some variables are already created by XWiki when you write a Velocity code, and you can use them.
109
110
111 This is some of them:
Vincent Massol 36.1 112
Guillaume Delhumeau 1.1 113 * //$hasEdit// : this variable tells us if the current user has the edit rights on the current page
114 * //$isGuest// : this variable tells us if the current user is a guest
115 * //$doc// : this variable represents the current document.
116 * //$xwiki// : this special variable is a tool to perform complicated stuffs
117 * //$request// : gives a lot of informations about the current request (like the referer, the query string, etc...)
118
119 === Methods ===
Vincent Massol 36.1 120
Clemens Robbenhaar 38.1 121 * Some variable are //objects//. An object can contain //variables// and //methods//.
Guillaume Delhumeau 1.1 122 * A method is like an //action// that the object can perform.
123 * Some actions give you an information, some just perform something.
124 * To call them, you write //$someObject.someMethod()// - with the '()'.
125
126 Examples:
127
Guillaume Delhumeau 32.2 128 {{code language="velocity"}}
Guillaume Delhumeau 1.1 129 ## will save the current document:
130 $doc.save()
131 ## will give you the content of the document:
132 $doc.getContent()
133 ## this method takes a "parameter", a text, which will be the new title:
134 $doc.setTitle("My new title")
135 ## will give you the current skin
136 $xwiki.getSkin()
Guillaume Delhumeau 32.2 137 {{/code}}
Guillaume Delhumeau 1.1 138
139 === Class ===
140
141 * Every object is different, they don't have the same variables and methods.
142 * But every object is an //instance// of a //class//.
Clemens Robbenhaar 38.1 143 * A //class// is like a model that describes what the object will have.
Guillaume Delhumeau 1.1 144 ** For example, the //Document// class has a //save// method, so every objects that are //instances// of //Document// will have the //save// method.
145 ** The //String// class has a //length// variable, so every instances of //String// will have this variable too.
146
Vincent Massol 36.1 147 === ===
148
Guillaume Delhumeau 1.1 149 * You can use the following code to know what is the class of an object:
150
Guillaume Delhumeau 32.2 151 {{code language="velocity"}}
Guillaume Delhumeau 1.1 152 #set($myVar = 42)
Clemens Robbenhaar 38.1 153 $myVar.class.name ## will display 'java.lang.Integer'
Guillaume Delhumeau 1.1 154 #set($myVar2 = "This is a text")
155 $myVar2.class.name ## will display 'java.lang.String'
156 $doc.class.name ## will display 'com.xpn.xwiki.api.Document'
157 $xwiki.class.name ## will display 'com.xpn.xwiki.api.XWiki'
Guillaume Delhumeau 32.2 158 {{/code}}
Guillaume Delhumeau 1.1 159
160
Lucas Charpentier (Sereza7) 43.1 161 {{image reference="class.jpg" width="350px"/}}
Guillaume Delhumeau 1.1 162
Vincent Massol 36.1 163 === ===
164
Guillaume Delhumeau 1.1 165 * Now that you know the class of your object, you can google it to have its documentation!
166 * With this documentation, you will be able to knows what method you can use.
167 ** Example with //java.lang.String//
168
Lucas Charpentier (Sereza7) 43.1 169 {{image reference="javadoc.png" width="650px"/}}
Guillaume Delhumeau 1.1 170
Vincent Massol 36.1 171 === ===
172
Clemens Robbenhaar 38.1 173 * As you may have noticed, the classes come from the //Java// world.
174 * The documentation about the java classes is called //Javadoc//.
Guillaume Delhumeau 1.1 175 * So you can use the the //Javadoc// for Velocity too!
176
Lucas Charpentier (Sereza7) 43.1 177 {{image reference="java.jpg" width="350px"/}}
Guillaume Delhumeau 1.1 178
179 == Basic instructions ==
Vincent Massol 36.1 180
Guillaume Delhumeau 1.1 181 === Conditions ===
Vincent Massol 36.1 182
Clemens Robbenhaar 38.1 183 * When you want to display something only under certain circumstances, you can use the //#if// command.
Guillaume Delhumeau 1.1 184
Guillaume Delhumeau 32.2 185 {{code language="velocity"}}
Guillaume Delhumeau 1.1 186 #if($isGuest)
187 Welcome visitor! You should subscribe to the wiki !
188 #end
Guillaume Delhumeau 32.2 189 {{/code}}
Guillaume Delhumeau 1.1 190
191 * You can also use the //#else// command:
192
Guillaume Delhumeau 32.2 193 {{code language="velocity"}}
Guillaume Delhumeau 1.1 194 #if($isGuest)
195 Welcome visitor! You should subscribe to the wiki !
196 #else
197 Hello $xcontext.user! I'm happy to see a registred user, I hate visitors!
198 #end
Guillaume Delhumeau 32.2 199 {{/code}}
Guillaume Delhumeau 1.1 200
201
Lucas Charpentier (Sereza7) 43.1 202 {{image reference="visitors.jpg" width="350px"/}}
Guillaume Delhumeau 1.1 203
204 === Conditions (bis) ===
Vincent Massol 36.1 205
Guillaume Delhumeau 1.1 206 * You can compare variables, with the following operators:
Clemens Robbenhaar 38.1 207 ** **//==//** : compare if the variables are equal
208 ** **//!=//** : say if the variables are different
Guillaume Delhumeau 1.1 209 ** **//>//** : say if the variable is superior to the other
210 ** **//<//** : do the oposite
211 ** **//>=//** : say if the variable is superior or equal to the other
212 ** **//<=//** : say if the variable is inferior or equal to the other
213
214 * You can create complicated conditions, with the logic operators:
215 ** **//&&//** : which means **"AND"**.
216 ** **//||//** : which means **"OR"**.
217 ** **//!//** : which means **"NOT"**.
218
219 === Examples ===
Vincent Massol 36.1 220
Guillaume Delhumeau 32.2 221 {{code language="velocity"}}
Guillaume Delhumeau 1.1 222 #set($a = 12)
223 #set($b = 28)
224
225 #if($a < $b)
226 A is inferior
227 #else
228 B is inferior
229 #end
230
231 #if($a < 20 && $b > 17)
232 A is inferior than 20 AND B is superior than 17
233 #end
234
235 #if($a == 15 || $b <= 2)
236 A equals 15 OR B is inferior than 2 or equal
237 #end
Guillaume Delhumeau 32.2 238 {{/code}}
Guillaume Delhumeau 1.1 239
Clemens Robbenhaar 38.1 240 === Examples (continued) ===
Vincent Massol 36.1 241
Guillaume Delhumeau 32.2 242 {{code language="velocity"}}
Guillaume Delhumeau 1.1 243 #set($c = true)
244
245 #if($c)
246 C is true
247 #end
248
249 #if($doc.isNew())
250 The document is New
251 #else
252 The document is an old document
253 #end
254
255 #if(!$doc.isHidden())
256 The document is NOT hidden
257 #end
Guillaume Delhumeau 32.2 258 {{/code}}
Guillaume Delhumeau 1.1 259
260 === Lists ===
Vincent Massol 36.1 261
Guillaume Delhumeau 1.1 262 * You can create a list of objects.
263
Guillaume Delhumeau 32.2 264 {{code language="velocity"}}
Guillaume Delhumeau 1.1 265 #set($myList = ['My first Item', 'My Second Item', 'My third item'])
Guillaume Delhumeau 32.2 266 {{/code}}
Guillaume Delhumeau 1.1 267
268 * You can add a new element in the list:
269
Guillaume Delhumeau 32.2 270 {{code language="velocity"}}
Guillaume Delhumeau 1.1 271 $myList.add('My fourth item')
Guillaume Delhumeau 32.2 272 {{/code}}
Guillaume Delhumeau 1.1 273
274 * You can know how many objects are inside the list:
275
Guillaume Delhumeau 32.2 276 {{code language="velocity"}}
Guillaume Delhumeau 1.1 277 $myList.size()
Guillaume Delhumeau 32.2 278 {{/code}}
Guillaume Delhumeau 1.1 279
280 * You can ask if an object is inside the list
281
Guillaume Delhumeau 32.2 282 {{code language="velocity"}}
Guillaume Delhumeau 1.1 283 $myList.contains('My Second Item') ## which tells me 'true' or 'false'
Guillaume Delhumeau 32.2 284 {{/code}}
Guillaume Delhumeau 1.1 285
Vincent Massol 36.1 286
Lucas Charpentier (Sereza7) 43.2 287 {{image reference="teacher.jpg" width="350px"/}}
Guillaume Delhumeau 1.1 288
289 === Example of lists ===
Vincent Massol 36.1 290
Guillaume Delhumeau 32.2 291 {{code language="velocity"}}
Guillaume Delhumeau 1.1 292 $doc.getComments() ## will give all the comments of the current page !
293 $doc.getAttachmentList() ## will give all the attachments of the current page
294 $xwiki.getSpaces() ## will give the list of the different spaces of the wiki
Guillaume Delhumeau 32.2 295 {{/code}}
Guillaume Delhumeau 1.1 296
297
298 How could I know the number of spaces in the wiki?
299
Guillaume Delhumeau 32.2 300 {{code language="velocity"}}
Guillaume Delhumeau 1.1 301 $xwiki.getSpaces().size()
Guillaume Delhumeau 32.2 302 {{/code}}
Guillaume Delhumeau 1.1 303
304
305 How could I know if the space 'BlaBla' exists?
306
Guillaume Delhumeau 32.2 307 {{code language="velocity"}}
Guillaume Delhumeau 1.1 308 $xwiki.getSpaces().contains('BlaBla')
Guillaume Delhumeau 32.2 309 {{/code}}
Guillaume Delhumeau 1.1 310
311
Lucas Charpentier (Sereza7) 43.1 312 {{image reference="goodmark.jpg" width="350px"/}}
Guillaume Delhumeau 1.1 313
314 === Foreach ===
Vincent Massol 36.1 315
Guillaume Delhumeau 1.1 316 * How can I display all the elements of a list in a nice way?
317 ** Use the //#foreach// loop, son.
Clemens Robbenhaar 38.1 318 * Example:
Guillaume Delhumeau 1.1 319
Guillaume Delhumeau 32.2 320 {{code language="velocity"}}
Guillaume Delhumeau 1.1 321 This is the list of all the spaces of the wiki
322 #set($spaceList = $xwiki.getSpaces())
323 #foreach($space in $spaceList)
324 * $space
325 #end
Guillaume Delhumeau 32.2 326 {{/code}}
Guillaume Delhumeau 1.1 327
328 * Results:
Lucas Charpentier (Sereza7) 43.1 329 {{image reference="foreach1.png" width="350px"/}}
Guillaume Delhumeau 1.1 330
331 == The API ==
332
Vincent Massol 36.1 333 === ===
334
Guillaume Delhumeau 1.1 335 * XWiki offers a lot of objects with a lot of methods, to perform a lot of things !
Sergei Kulagin 38.3 336 ** Delete a document, add a tag to another document, display the list of all blog entries, handle complex formulars, etc...
Clemens Robbenhaar 38.1 337 * This objects & methods are called the **API** (as //Application Programming Interface//).
Guillaume Delhumeau 1.1 338 * But how can I know what are theses objects and how to use it?
339
Lucas Charpentier (Sereza7) 43.1 340 {{image reference="API.jpg" width="650px"/}}
Guillaume Delhumeau 1.1 341
342 === By using the XWiki Script Reference Documentation (SRD) ===
343
Lucas Charpentier (Sereza7) 43.1 344 {{image reference="SRD.png" width="960px"/}}
Guillaume Delhumeau 1.1 345
Vincent Massol 37.7 346 Go to the [[SRD>>xwiki:ScriptingDocumentation.WebHome]].
Guillaume Delhumeau 1.1 347
348 === SRD ===
Vincent Massol 36.1 349
Guillaume Delhumeau 1.1 350 * The left column lists **all the XWiki special variables that make the API**.
351 * Select one of them, and you will have **all the methods this special variable offers**
Clemens Robbenhaar 38.1 352 ** Examples:
Guillaume Delhumeau 1.1 353 *** //$xwiki// offers a method //getDocument()//
354 *** //$doc// has a method //addAttachment()//
355 *** etc...
356
Vincent Massol 36.1 357 === Is the API a mess? ===
358
Clemens Robbenhaar 38.1 359 * While looking at the SRD, we can ask youself //"how many special variables are there?"//
Guillaume Delhumeau 1.1 360 * When we create a variable for our own usage, we have to be sure first to not call it as an existing special variable.
361 ** It's a mess!!!
362
Lucas Charpentier (Sereza7) 43.1 363 {{image reference="mess.jpg" width="650px"/}}
Guillaume Delhumeau 1.1 364
365 === Introducing Script Services ===
Vincent Massol 36.1 366
Guillaume Delhumeau 1.1 367 * In order to make the things cleaner, the plan is to remove a lot of this special variables and replace them by //script services//.
368 * Script services will become the only API in future.
369 * All script services are prefixed by //$services//.
370
Lucas Charpentier (Sereza7) 43.1 371 {{image reference="datacenter.jpg" width="650px"/}}
Guillaume Delhumeau 1.1 372
Vincent Massol 36.1 373 === ===
374
Guillaume Delhumeau 1.1 375 * To use a script service, you do it this way:
376
Guillaume Delhumeau 32.2 377 {{code language="velocity"}}
Guillaume Delhumeau 1.1 378 $services.query ## to perform complicated researchs
379 $services.localization ## to take care of the languages
380 $services.officeimporter ## to import office documents
381 ...
Guillaume Delhumeau 32.2 382 {{/code}}
Guillaume Delhumeau 1.1 383
384 * Of course, it is documented on the SRD!
385
Lucas Charpentier (Sereza7) 43.1 386 {{image reference="wayne.jpg" width="350px"/}}
Guillaume Delhumeau 1.1 387
Vincent Massol 36.1 388 === ===
389
Guillaume Delhumeau 1.1 390 * Sometime, you have different ways to do the same thing:
391 ** Using an old method (which may be declared as //deprecated//).
392 ** Using a script service instead
393 * ** Always perfers to use Script Service instead of deprecated APIs!!!**
394
Guillaume Delhumeau 32.2 395 {{code language="velocity"}}
Guillaume Delhumeau 1.1 396 $xwiki.searchDocument() ## allow us to perfom a query with the HQL language
397 ## now please use:
398 $services.query.hql() ## allow us to perfom a query with the HQL language
399 ## or even better:
400 $services.query.xwql() ## XWQL language is easier to use
Guillaume Delhumeau 32.2 401 {{/code}}
Guillaume Delhumeau 1.1 402
403 * Because it's the future!
404
Lucas Charpentier (Sereza7) 43.1 405 {{image reference="future.jpg" width="350px"/}}
Guillaume Delhumeau 1.1 406
407 == Manipulation of XObjects ==
Vincent Massol 36.1 408
Guillaume Delhumeau 1.1 409 === What is an XObject ? ===
Vincent Massol 36.1 410
Guillaume Delhumeau 1.1 411 * An //XObject// is an **object** that you can **save** in the wiki.
Lucas Charpentier (Sereza7) 40.1 412 * An XObject is attached to a document, and you can see them with the "objects editor" on the wiki. {{warning}} If you can't see the object editor, it's probably because you are not registered as an advanced user on your wiki. In order to become an advanced user, use the shortcut keys x+x+x+a or go into your profile, section ##Preferences##. This complex editor is hidden from simple users :) {{/warning}}
Guillaume Delhumeau 1.1 413
Lucas Charpentier (Sereza7) 43.1 414 {{image reference="edit_objects.png" width="350px"/}}
Guillaume Delhumeau 1.1 415
Vincent Massol 36.1 416 === ===
417
Lucas Charpentier (Sereza7) 43.1 418 {{image reference="objects.png" width="960px"/}}
Guillaume Delhumeau 1.1 419
420 === XClass ===
Vincent Massol 36.1 421
Guillaume Delhumeau 1.1 422 * An //XClass// is a //class// (a //description//) for XObjects.
423 ** You can define them using the class editor.
424 ** Or by using //Application Within Minutes//!
425
426 === Class Editor ===
Vincent Massol 36.1 427
Lucas Charpentier (Sereza7) 43.1 428 {{image reference="class_editor.png" width="960px"/}}
Guillaume Delhumeau 1.1 429
430 === Application Within Minutes ===
Vincent Massol 36.1 431
Lucas Charpentier (Sereza7) 43.1 432 {{image reference="appwithinminutes.png" width="960px"/}}
Guillaume Delhumeau 1.1 433
434 === Why using XClass and XObjects? ===
Vincent Massol 36.1 435
Guillaume Delhumeau 1.1 436 * It allows us to store **structured** informations.
437 ** A //Blog Post// (with a //title//, a //date//, a //content//, etc...)
438 ** An //Evaluation document// (with a //remark// for each //category//, a //mark//, etc...)
439 ** An //Holiday Request// (with //dates//, //status//, etc...)
440
441 * So the //information// is not lost in a giant document that holds only a big text...
442 ** And you can create //Applications//!
443
444 === XClass is used //everywhere// ===
Vincent Massol 36.1 445
Guillaume Delhumeau 1.1 446 * Almost everything in XWiki is an XClass and stored as XObjects!
447 ** //Users//
448 ** //Groups//
449 ** //Comments//
450 ** //Tags//
451 ** //Access Rights//
452 ** ....
453 * Learn to use //XClass// and you can **control all your wiki**.
454
455 === A comment is an XObject ===
Vincent Massol 36.1 456
Lucas Charpentier (Sereza7) 43.1 457 {{image reference="comments.png" width="960px"/}}
Guillaume Delhumeau 1.1 458
459 === A user is an XObject ===
Vincent Massol 36.1 460
Lucas Charpentier (Sereza7) 43.1 461 {{image reference="user.png" width="960px"/}}
Guillaume Delhumeau 1.1 462
463 === How to get an XObject with Velocity? ===
464
Guillaume Delhumeau 32.2 465 {{code language="velocity"}}
Guillaume Delhumeau 1.1 466 ## First, get the object
467 #set($object = $doc.getObject('XWiki.XWikiUsers'))
468 ## The parameter is the name of the class of the object you want to have
469 ## (a document can hold multiple objects of different classes)
470 ## The class name is the Document name where the class is defined
471
472 ## Display a parameter:
Clemens Robbenhaar 38.2 473 $object.get('first_name')
Guillaume Delhumeau 1.1 474 ## the parameter is the name of the field you want to display
475 ## if you are in "edit" mode, an HTML field will be displayed instead of the object value.
476
477 ## Set values:
478 $object.set('last_name', 'Bond')
479 $object.set('first_name', 'James')
480
481 ## Then save the document if you want your changes to be conserved
482 ## (you can add a comment when you save)
Clemens Robbenhaar 38.1 483 $doc.save('I have changed the name of the user.')
Guillaume Delhumeau 32.2 484 {{/code}}
Guillaume Delhumeau 1.1 485
Clemens Robbenhaar 38.1 486 === You can rollback what your script has done ===
Vincent Massol 36.1 487
Lucas Charpentier (Sereza7) 43.1 488 {{image reference="rollback.png" width="350px"/}}
Guillaume Delhumeau 1.1 489
490 == Tips ==
491
Sergei Kulagin 38.3 492 === How to get a document? ===
Vincent Massol 36.1 493
Guillaume Delhumeau 1.1 494 * In the past, we used to do:
495
Guillaume Delhumeau 32.2 496 {{code language="velocity"}}
Clemens Robbenhaar 38.1 497 #set($document = $xwiki.getDocument('Space.Page'))
Guillaume Delhumeau 32.2 498 {{/code}}
Guillaume Delhumeau 1.1 499
500 * But it's not good, because what if the page name has a dot?
501
Guillaume Delhumeau 32.2 502 {{code language="velocity"}}
Guillaume Delhumeau 1.1 503 #set($document = $xwiki.getDocument('Space.Pa.ge'))
Clemens Robbenhaar 38.1 504 ## ??? What is the Space ? What is the Page ?
Guillaume Delhumeau 32.2 505 {{/code}}
Guillaume Delhumeau 1.1 506
507 * And what will happen if the document name have accents or weird characters?
508
Vincent Massol 36.1 509 === Solution: use references ! ===
Guillaume Delhumeau 1.1 510
Guillaume Delhumeau 32.2 511 {{code language="velocity"}}
Guillaume Delhumeau 1.1 512 #set($reference = $services.model.createDocumentReference('Wiki', 'Space', 'Page'))
Clemens Robbenhaar 38.1 513 #set($document = $xwiki.getDocument($reference))
Guillaume Delhumeau 32.2 514 {{/code}}
Guillaume Delhumeau 1.1 515
Clemens Robbenhaar 38.1 516 * It is better! The reference tool allow us to strictly separate what is the space and what is the page
Guillaume Delhumeau 1.1 517 * And the reference tool will take care of weird characters !
518
Guillaume Delhumeau 32.2 519 {{code language="velocity"}}
Guillaume Delhumeau 1.1 520 #set($reference = $services.model.createDocumentReference('Wiki', 'Space', 'Pa.ge'))
Clemens Robbenhaar 38.1 521 #set($document = $xwiki.getDocument($reference))
Guillaume Delhumeau 1.1 522 ## Not a problem!!!
Guillaume Delhumeau 32.2 523 {{/code}}
Guillaume Delhumeau 1.1 524
525 === References ===
Vincent Massol 36.1 526
Guillaume Delhumeau 1.1 527 * You can get create references for documents in other wikis :
528
Guillaume Delhumeau 32.2 529 {{code language="velocity"}}
Guillaume Delhumeau 1.1 530 #set($reference = $services.model.createDocumentReference('Wiki', 'Space', 'Page'))
Guillaume Delhumeau 32.2 531 {{/code}}
Guillaume Delhumeau 1.1 532
533 * If you want to get a document in the current wiki, you can set an empty parameter:
534
Guillaume Delhumeau 32.2 535 {{code language="velocity"}}
Guillaume Delhumeau 1.1 536 #set($reference = $services.model.createDocumentReference('', 'Space', 'Page'))
Clemens Robbenhaar 38.1 537 ## the reference point to a document in the current wiki
Guillaume Delhumeau 32.2 538 {{/code}}
Guillaume Delhumeau 1.1 539
Guillaume Delhumeau 27.2 540 === Use case: changing the name of a user ===
Guillaume Delhumeau 1.1 541
Guillaume Delhumeau 32.2 542 {{code language="velocity"}}
Clemens Robbenhaar 38.1 543 ## First, create a reference to their user page
Guillaume Delhumeau 1.1 544 #set($reference = $services.model.createDocumentReference('', 'XWiki', 'ldubost'))
545 ## Then, get its document
546 #set($document = $xwiki.getDocument($reference))
547 ## Then, get the 'User' object
548 #set($object = $document.getObject('XWiki.XWikiUsers'))
Clemens Robbenhaar 38.1 549 ## Then change their name
Guillaume Delhumeau 1.1 550 $object.set('first_name', 'Harry')
551 $object.set('last_name', 'Potter')
552 ## And save the document
553 $document.save()
Guillaume Delhumeau 32.2 554 {{/code}}
Guillaume Delhumeau 1.1 555
Guillaume Delhumeau 27.2 556 === But remember we know who changes what! ===
Vincent Massol 36.1 557
Lucas Charpentier (Sereza7) 43.1 558 {{image reference="history.png" width="350px"/}}
Guillaume Delhumeau 1.1 559
560 (% style="text-align: right; margin-top: 20px;" %)
561 "//I have your name and I know where you live...//"
562
563 == Going further ==
Vincent Massol 36.1 564
565 === ===
566
Guillaume Delhumeau 1.1 567 * See the [[Velocity documentation>>http://velocity.apache.org/]] to have more details about the language itself
Vincent Massol 37.1 568 * See the [[XWiki Script Guide>>doc:Documentation.DevGuide.Scripting.WebHome]] to have some informations about how to make scripts in XWiki
Vincent Massol 36.3 569 * See the [[XWiki Query Module>>doc:extensions:Extension.Query Module]] to learn how to do advanced queries
Ecaterina Moraru (Valica) 36.4 570 * See the [[XWiki Data Model>>doc:Documentation.DevGuide.DataModel]] to learn how to create your XWiki classes
Guillaume Delhumeau 1.1 571
572 **But in general:**
Vincent Massol 36.1 573
Guillaume Delhumeau 1.1 574 * practice and ask for help!!!
575
Vincent Massol 36.1 576 === ===
577
Guillaume Delhumeau 1.1 578 * I hope you now have a better idea of what Velocity is and how you can use it in XWiki.
579
Lucas Charpentier (Sereza7) 43.1 580 {{image reference="velocity.jpg" width="960px"/}}

Get Connected