Version 8.1 by Andy Tripp on 2020/06/10

Hide last authors
Andy Tripp 4.1 1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
Andy Tripp 1.1 4
Andy Tripp 4.1 5 This tutorial will show you how to build a Contact Manager application.  It's very similar to the [[Documentation.DevGuide.Tutorials.FAQTutorial.FAQTutorialManual]] tutorial. The only difference (so far) is that the FAQ tutorial involves a very trivial object: a FAQ object contains just two things: a "question" and an "answer", while this example uses a Person object that has more fields ("name", "age", "sex", etc.) .This is a very simple application that makes use of XWiki's [[classes, properties, and objects>>platform:DevGuide.DataModel||anchor="HXWikiClasses2CObjects2CandProperties"]]. It also uses a technique that you may frequently use as the basis for several different kinds of applications.
Andy Tripp 1.1 6
7 Also, this tutorial explains things a little differently than the FAQ tutorial. If something here doesn't make sense to you, try going through the FAQ tutorial and maybe it will make more sense.
8
Andy Tripp 4.1 9 = Prerequisites for following the tutorial =
Andy Tripp 1.1 10
Andy Tripp 4.1 11 You should have [[installed XWiki>>platform:AdminGuide.Installation]] and have a [[basic understanding of how to use it>>platform:Features.WebHome]].
Andy Tripp 1.1 12
Andy Tripp 4.1 13 All through this tutorial you should refer to the [[XWiki Data Model>>platform:DevGuide.DataModel]] for information on XWiki's data model. You might also use the [[XWiki Scripting Guide>>platform:DevGuide.Scripting]] to get you started with scripting in XWiki and manipulating XWiki objects. In addition, this tutorial will introduce the concepts of Authoring Templates and Page Design Sheets, patterns that you will find particularly useful in creating XWiki applications. Completing this tutorial is a recommended prerequisite for anyone who wants to build custom applications on the XWiki engine. And by "custom application", we mean a huge variety of web applications.
Andy Tripp 1.1 14
Andy Tripp 4.1 15 {{warning}}
16 Make sure that your user is an [[Advanced user>>platform:Features.PageEditing||anchor="HAdvancedMode"]] before following this tutorial since you'll need for example to use the wiki editor (##Wiki > Edit## menu).
17 {{/warning}}
Andy Tripp 1.1 18
Andy Tripp 4.1 19 = Application Overview =
Andy Tripp 1.1 20
Andy Tripp 4.1 21 The Person Manager application will allow users to create a "Person object" by entering data (name, age, sex, etc) into a simple form field and then submitting the form. Let's sketch out what roughly what those two pages should look like on a "napkin", using [[Balsamiq>>https://balsamiq.com/]]:
Andy Tripp 1.1 22
Andy Tripp 4.1 23 image:personDialog.PNG
Andy Tripp 1.1 24
Andy Tripp 4.1 25 Our page won't look exactly like that, but the point is that a website user can create a Person by filling out a "form" page like this.
Andy Tripp 1.1 26
Andy Tripp 4.1 27 The Person then appears in a table with all other Person objects that have been previously created. Users can click on the Person in the table to see all the information about the Person. He may also edit that information or delete the Person. The table might look something like this:
Andy Tripp 1.1 28
Andy Tripp 4.1 29 image:personTable.PNG
Andy Tripp 1.1 30
Andy Tripp 4.1 31 When the user clicks on a row in the table, he will get a page that shows the information about the Person, which will look similar to the "Create Person" page, but without the ability to change anything.
Andy Tripp 1.1 32
Andy Tripp 4.1 33 == Objects Overview And Terminology ==
Andy Tripp 1.1 34
Andy Tripp 7.1 35 Next, let's summarize the terminology for "Objects". For full details, see [[Data Model>>https://www.xwiki.org/xwiki/bin/view/Documentation/DevGuide/DataModel/#HXWikiClasses2CObjects2CandProperties]]. There is nothing fancy happening here, but it's important to get our terminology straight.
Andy Tripp 1.1 36
Andy Tripp 7.1 37 A Person is an example of some "Object" or "class". We will use XWiki to define what "fields" (or "properties") are in a "Person" object. For example, we will say that there's a field called "name" of type "String". There's also a field called "age" of type "Number", and an "address" field that's type "TextArea" (a string that can be multiple lines.
38
Andy Tripp 4.1 39 When a user creates a new Person, we call that an "instance" of the "class". So we might say something like "I've created an instance of the Person class, with name 'Joe Smith'". And we would say that our table shows all the instances of the Person class. And instances can not only be created, but also edited or deleted.
Andy Tripp 1.1 40
Andy Tripp 7.1 41 We, the creators of the website, define the "Person" class. We define that once, and we're done. Our users use our website to create, edit, and delete instances of our Person class.
42
Andy Tripp 4.1 43 == Overview Of What We Will Do ==
Andy Tripp 7.1 44
Andy Tripp 4.1 45 In this tutorial, we'll do the following steps:
Andy Tripp 1.1 46
Andy Tripp 4.1 47 * Define our Person class with all it's fields, using the XWiki "Class Editor".
48 * Define how a Person instance should be displayed, by creating a "Person Sheet" page.
49 * Create a Template and a Template Provider (whatever they are) for our Person class.
Andy Tripp 7.1 50 * Create a web page that displays a table of people.
Andy Tripp 4.1 51 * Create a web page that displays a Person instance.
Andy Tripp 1.1 52
Andy Tripp 7.1 53 Note that we don't need to define a page for creating or editing a Person, just a page for *displaying* a Person. XWiki will automatically do that for us!
Andy Tripp 1.1 54
Andy Tripp 7.1 55 Once we are done with these steps, our application will be finished. A user of our website can see a page containing the table of Person objects, view the page for an existing person, add a new Person, edit an existing Person, or delete a Person.
Andy Tripp 1.1 56
Andy Tripp 7.1 57 = Go to the Special "Data Types" Page =
Andy Tripp 1.1 58
Andy Tripp 7.1 59 The "Data Types" page is a special XWiki page that lets us define classes like "Person". It'sactually hidden by default. To be "unhide" it, go to your profile page, select the Preferences tab, edit the page and choose to view Hidden Documents.
Andy Tripp 4.1 60
Andy Tripp 7.1 61 To find thec"Data Types" page, enter a search query for the keyword "D". This should return a document titled "Data Types".
62
Andy Tripp 4.1 63 = Create the Person Class =
64
Andy Tripp 7.1 65 * On the "Data Types" page, under the heading "Create a new data type", in the "Title" field, enter ##Person## as the name of the page to create:(((
66 {{image reference="personClass.PNG"/}}
Andy Tripp 4.1 67 )))
Andy Tripp 7.1 68 * As you can see in the Breadcrumb below the new page will be created at location ##XWiki > Person##. In practice the "Data Types" page will automatically prefix the page name with ##Class## (you could also enter ##PersonClass## as the page name directly).
69 * Now it would be nice to have it created in a new location such as ##PersonSpace > Person Class##. Since the ##PersonSpace## parent doesn't exist we cannot use the Tree picker button. Thus click the Pencil button as shown in the following image and replace ##XWiki## by ##PersonSpace##.(((
70 {{image reference="personLocation.PNG"/}}
Andy Tripp 4.1 71 )))
Andy Tripp 7.1 72 * In technical terms you're creating a page named ##PersonClass## (with a title of "Person Class") located in a space also called ##PersonSpace## and thus the technical reference is ##PersonSpace.PersonClass##.
Andy Tripp 4.1 73 * Click the "Create this Class" button. You should then see a page with the following content:(((
74 {{code language="none"}}
75 {{velocity}}
76 ## Replace the default space with the space where you want your documents to be created.
77 ## Replace the default parent with the one of your choice and save the document.
78 ##
Andy Tripp 1.1 79 #set($defaultSpace = $doc.space)
80 #set($defaultParent = $doc.fullName)
Andy Tripp 4.1 81 {{/velocity}}
82 {{/code}}
83 )))
Andy Tripp 1.1 84
Andy Tripp 7.1 85 In the code, change the word "$doc.space" with the name of the space where you want your pages to be created: "PersonSpace".
Andy Tripp 1.1 86 The line of code should look like this:
87
Andy Tripp 4.1 88 {{code language="none"}}
Andy Tripp 7.1 89 #set($defaultSpace = 'PersonSpace')
Andy Tripp 4.1 90 {{/code}}
Andy Tripp 1.1 91
92 You can also change the default parent of the new FAQ documents that are going to be created. To do so, replace the "$defaultParent" variable with the name of your document.
93 The line of code should look like this:
94
Andy Tripp 4.1 95 {{code language="none"}}
Andy Tripp 7.1 96 #set($defaultParent = 'PersonSpace.WebHome')
Andy Tripp 4.1 97 {{/code}}
Andy Tripp 1.1 98
Andy Tripp 7.1 99 Click the "Save & View" button. The class is now created and you should be looking at a page titled "Class: Person" that looks like this:
Andy Tripp 1.1 100
Andy Tripp 4.1 101 {{image reference="FAQClass1.PNG"/}}
Andy Tripp 1.1 102
Andy Tripp 4.1 103 = Add Properties to the Class =
Andy Tripp 1.1 104
Andy Tripp 4.1 105 Under the page title, you should see the words "The class does not have any properties yet. You can use the //__class editor__// to define them." Let's just follow those instructions!
Andy Tripp 1.1 106
Andy Tripp 4.1 107 * Click on the 'class editor' link
108 * Note that the link trail in the header is something like "Wiki Home / FAQ / FAQ Class". This shows you that you are indeed on the FAQ class page in Class edit mode.
Andy Tripp 1.1 109
Andy Tripp 4.1 110 In our document, we'll store both a //question// and an //answer//. So we need to create a property for each of them.
Andy Tripp 1.1 111
Andy Tripp 4.1 112 * Enter the text //question// in the "name" field
113 * Choose a TextArea type for the property and then click on "Add". The TextArea will ultimately give us a multi-line text field in our authoring template.(((
114 {{image reference="AddQuestionProperty.PNG"/}}
115 )))
116 * Click on the "+" icon to expand the options for the newly created property
117 * Change the value of the "Pretty Name" field to "Question"(capital Q):(((
118 {{image reference="QuestionProperty.PNG"/}}
119 )))
120 * Now create another property called //answer// the same way that you did for the previous "question" property (choose TextArea for the property type)
121 * Expand it from the property list and change the value of the "Pretty Name" field to "Answer"
122 * When you are done adding and configuring the properties, click the "Save & View" button
Andy Tripp 1.1 123
Andy Tripp 4.1 124 = Create the Page Design Sheet =
Andy Tripp 1.1 125
Andy Tripp 4.1 126 * After the previous step you are now on the FAQClass page which should look like this:(((
127 {{image reference="FAQClass2.PNG"/}}
128 )))
129 * Click the first button ("Create the document sheet") to create the document sheet (the Page Design Sheet). This sheet determines how your page's objects will be rendered to the user. The document is automatically created.
130 * You should see a warning message with the text "The sheet is not bound to the class so it won't be applied automatically when a page that has an object of this class is displayed". Click the "Bind the sheet to the class" link that appears after the text. **What this does is important**:
131 ** It adds an object of type ##XWiki.ClassSheetBinding## to the ##FAQ.FAQClass" document. Basically it ties the FAQ Class to the Sheet.##
132 ** **It's because of this object that users will be sent to form edition mode when editing FAQ entries**
133 * Now click on "View the sheet document". This takes you to the ##FAQ.FAQSheet## page which you can edit in wiki mode and see its default content:(((
134 {{code language="none"}}
135 {{velocity}}
136 ## You can modify this page to customize the presentation of your object.
137 ## At first you should keep the default presentation and just save the document.
Andy Tripp 1.1 138
Andy Tripp 4.1 139 ## We have to use wiki=true because $doc.display() can produce wiki syntax.
140 {{html wiki="true" clean="false"}}
141 ## Load the JavaScript code required to make the object properties editable in-place.
Andy Tripp 1.1 142 #set ($discard = $xwiki.jsfx.use('uicomponents/edit/editableProperty.js', {
Andy Tripp 4.1 143   'forceSkinAction': true,
144   'language': $xcontext.locale
Andy Tripp 1.1 145 }))
146 #set ($editing = $xcontext.action == 'edit')
Andy Tripp 4.1 147 ## The object to display.
Andy Tripp 1.1 148 #set ($xobject = $doc.getObject('FAQ.FAQClass'))
Andy Tripp 4.1 149 ## The class that describes the object properties.
Andy Tripp 1.1 150 #set ($xclass = $xobject.xWikiClass)
Andy Tripp 4.1 151 ## Make sure the following display* method calls use the right object.
Andy Tripp 1.1 152 #set ($discard = $doc.use($xobject))
Andy Tripp 4.1 153 ## Using the xform vertical form layout.
Andy Tripp 1.1 154 <div class="xform">
Andy Tripp 4.1 155   <dl>
156   #foreach ($property in $xclass.properties)
157     <dt #if (!$editing && $hasEdit)
158         class="editableProperty"
159         #set ($xobjectPropertyReference = $xobject.getProperty($property.name).reference)
160         data-property="$escapetool.xml($services.model.serialize($xobjectPropertyReference))"
161         data-property-type="object"#end>
162       ## This must match the id generated by the $doc.display() method below.
163       #set ($propertyId = "${xclass.name}_${xobject.number}_$property.name")
164       <label#if ($editing) for="$escapetool.xml($propertyId)"#end>
165         $escapetool.xml($property.translatedPrettyName)
166       </label>
167       ## Support for specifying a translation key as hint in the property definition.
168       <span class="xHint">$!escapetool.xml($services.localization.render($property.hint))</span>
169     </dt>
170     <dd>$doc.display($property.name)</dd>
171   #end
172   #if (!$xclass.properties || $xclass.properties.size() == 0)
173     ## Keep the empty definition term in order to have valid HTML.
174     <dt></dt>
175     <dd>$escapetool.xml($services.localization.render('xclass.defaultObjectSheet.noProperties'))</dd>
176   #end
177   </dl>
Andy Tripp 1.1 178 </div>
Andy Tripp 4.1 179 {{/html}}
180 {{/velocity}}
181 {{/code}}
Andy Tripp 1.1 182
183 Let's take a moment now and analyze this code:
184
Andy Tripp 4.1 185 * first, it loads the JavaScript required for in-place editing
186 * then it retrieves the FAQ object from the current page and the FAQ class defintion
187 * then it iterates through all the FAQ class properties displaying:
188 ** the translated pretty name
189 ** the translated hint
190 ** the value from the FAQ object in view mode, or the form field used to input or modify the value in edit mode
191 * it uses a definition list to display the FAQ object, following the [[Vertical Form>>xwiki:Documentation.DevGuide.FrontendResources.VerticalForms.WebHome]] layout
Andy Tripp 1.1 192
Andy Tripp 4.1 193 As we mentioned, XWiki provides a mechanism that helps us create sheets used for both view and edit mode. This is the ##display## function used in the line:
Andy Tripp 1.1 194
Andy Tripp 4.1 195 {{code language="none"}}
Andy Tripp 1.1 196 <dd>$doc.display($property.name)</dd>
Andy Tripp 4.1 197 {{/code}}
Andy Tripp 1.1 198
199 It detects the current mode (edit or view) and displays the property referenced by its argument as the mode dictates:
200
Andy Tripp 4.1 201 * in view mode it will display the value of the property
202 * in edit mode it will display a form field that will allow the user to edit it
Andy Tripp 1.1 203
Andy Tripp 4.1 204 This way we can use a single Design Sheet to both display and edit our FAQ entries. See the [[XWiki API reference>>platform:DevGuide.API]] and [[XWiki Scripting>>platform:DevGuide.Scripting]] pages for more details about this.
205 )))
206 * Click "Save & View"
Andy Tripp 1.1 207
Andy Tripp 4.1 208 = Create the Authoring Template =
Andy Tripp 1.1 209
Andy Tripp 4.1 210 * Navigate back to the ##FAQ.FAQClass## document (you can use the arrows in the breadcrumb to do so). The document should look like this:(((
211 {{image reference="FAQClass3.PNG"/}}
212 )))
213 * Click on the "Create the document template" button. The Authoring Template will be automatically created.
Andy Tripp 1.1 214
Andy Tripp 4.1 215 Note that earlier, we changed the space name preceding the page name because we wanted all of our FAQ pages to reside in a space named FAQ. Remember that all our documents will be copies of the Authoring Template used as a prototype so the content will be copied in all our FAQs documents and will execute the Design Sheet code in the context of the current document. See the [[dedicated page>>platform:DevGuide.IncludeInVelocity]] for more information regarding this technique.
Andy Tripp 1.1 216
217 Now we need to associate the prototype object with this document to turn it into a true authoring template:
218
Andy Tripp 4.1 219 * If you're on the template page, navigate back to the ##FAQ.FAQClass## document.
220 * At the bottom of the page, look for the following warning message: "The template does not contain an object of type FAQClass. Add a FAQ object to the template »."
221 * Click on "Add a FAQ object to the template »":(((
222 {{image reference="FAQObject.png"/}}
223 )))
Andy Tripp 1.1 224
225 Next, we want to remove the title for the newly created template:
226
Andy Tripp 4.1 227 * Navigate to the ##FAQ.FAQTemplate## document (you can click on the "View the template page (FAQ / FAQ Template)" link for doing that for example.
228 * Edit this document in Wiki mode
229 * Inside the Title field you have "FAQ Template" written -> delete this text
230 * Save & View
Andy Tripp 1.1 231
232 This step is needed so that all of our future entries don't have "FAQ Template" as their title.
233
234 Congratulations: you just created an Authoring Template! You're almost done now.
235
Andy Tripp 4.1 236 {{image reference="FAQClass4.PNG"/}}
Andy Tripp 1.1 237
Andy Tripp 4.1 238 = Create the Template Provider =
Andy Tripp 1.1 239
240 After the template was created and the object was added, a new section appears with a button to create a template provider to use the existing template.
241 The template provider is created with some default values, but those can be updated to match the needs.
242
Andy Tripp 4.1 243 {{gallery}}
244 image:3-missingTemplateProvider.png
245 image:4-addedTemplateProvider.png
246 image:5-templateProviderView.png
247 {{/gallery}}
Andy Tripp 1.1 248
249
250
Andy Tripp 4.1 251 = Create a home page for the FAQ application =
252
Andy Tripp 1.1 253 You want your users to be able to see a list of all existing questions and answers and to add new questions. The best way to do this is to put the FAQ application in its own space and to use that space's homepage to display existing questions.
254
Andy Tripp 4.1 255 Thus we now need to create the ##FAQ.WebHome## page
Andy Tripp 1.1 256
Andy Tripp 4.1 257 * Click on "FAQ" in the breadcrumb to navigate to ##FAQ.WebHome## and notice that the page doesn't exist.
258 * Edit it in wiki mode
259 * Type in the title "FAQs"
Andy Tripp 1.1 260
Andy Tripp 4.1 261 == Displaying existing FAQ entries ==
Andy Tripp 1.1 262
263 You have 2 options when it comes to displaying existing FAQ entries:
264
Andy Tripp 4.1 265 1. Use the livetable component
266 1. Write custom code in order to display them
Andy Tripp 1.1 267
Andy Tripp 4.1 268 === Using the Livetable component ===
Andy Tripp 1.1 269
Andy Tripp 4.1 270 In this section, we will show how to use the [[Livetable Macro>>extensions:Extension.Livetable Macro]] to display the existing questions and answers.
Andy Tripp 1.1 271
272 The livetable component will give users the ability to easily list and filter through existing FAQ documents. The macro is made of 3 parts:
273
Andy Tripp 4.1 274 * The list of columns: for each entry, we will display the question, the date when it was created and a special column that lets users quickly modify entries
275 * The properties of each column: for each column, we will define how it should be displayed, whether it should link to the entry and whether it should be filterable
276 * The livetable options: those are options related to the display of the livetable (in this case we will to display a tag cloud and 10 rows by default)
Andy Tripp 1.1 277
278 Here is the resulting code:
279
Andy Tripp 4.1 280 {{code language="none"}}
281 {{velocity}}
Andy Tripp 1.1 282 #set($columns = ["question", "doc.creationDate", "_actions"])
283 #set($columnsProperties = {
Andy Tripp 4.1 284     "question" : { "type" : "text", "link" : "view", "html" : "true", "sortable":true },
285     "_actions" : {"actions": ["edit","delete"]}
Andy Tripp 1.1 286 })
287 #set($options = {
Andy Tripp 4.1 288    "className":"FAQ.FAQClass",
289    "translationPrefix" : "faq.",
290    "tagCloud" : true,
291    "rowCount": 10
Andy Tripp 1.1 292 })
293 #livetable("faq" $columns $columnsProperties $options)
Andy Tripp 4.1 294 {{/velocity}}
295 {{/code}}
Andy Tripp 1.1 296
Andy Tripp 4.1 297 * Copy this code and paste it as Wiki content (inside ##FAQ.WebHome##)
298 * Click "Save and View"
299 * New FAQ entries will now be displayed on the page once you create them
Andy Tripp 1.1 300
Andy Tripp 4.1 301 The ##FAQ.WebHome## page should look similar to this:
Andy Tripp 1.1 302
Andy Tripp 4.1 303 {{image reference="FAQsLivetable.png"/}}
Andy Tripp 1.1 304
305 Notice how there are some translation keys displayed inside the livetable. Let's create a translations document and change those keys to actual text:
306
Andy Tripp 4.1 307 * Create a new page inside the FAQ space called Translations, i.e. at ##FAQ.Translations## (using the "+" button)
308 * Edit it in Wiki mode and paste this content inside:(((
309 {{code language="none"}}
Andy Tripp 1.1 310 faq.question=Question
311 faq.doc.creationDate=Creation Date
312 faq._actions=Actions
Andy Tripp 4.1 313 {{/code}}
314 )))
315 * Click "Save & View"
316 * Edit it again in Object mode and add a new ##XWiki.TranslationDocumentClass## object to mark the page as a page containing translations. Make sure to select the right scope. Using "Global" means the translations would be visible to all wikis (and it requires having Programming Rights!) and "Wiki" means it's visible to the current wiki only. Choose "Wiki" as the scope.
317 * Click "Save & View" again
Andy Tripp 1.1 318
Andy Tripp 4.1 319 Now the ##FAQ.WebHome## page should look like this (notice the translations instead of the keys):
Andy Tripp 1.1 320
Andy Tripp 4.1 321 {{image reference="FAQsLivetableWithTranslations.png"/}}
Andy Tripp 1.1 322
Andy Tripp 4.1 323 === Using custom code ===
Andy Tripp 1.1 324
325 You will need to write the following code:
326
Andy Tripp 4.1 327 * A XWQL query that will find all your FAQ documents. Check the [[Query API documentation>>extensions:Extension.Query Module]] to know more about it.
328 ** The XWQL query looks for all documents that have a ##FAQ.FAQClass## object other than the template
329 ** If no document has been created yet, a warning message is displayed
330 * A piece of velocity code to display all those documents
331 ** The velocity code loops in that list
332 ** For each item, the full document is loaded in memory so that values can be retrieved from it
333 ** For each document, the question is retrieved and displayed as a link towards the FAQ entry
Andy Tripp 1.1 334
335 Here is the resulting code:
336
Andy Tripp 4.1 337 {{code language="none"}}
338 = Existing FAQ entries =
Andy Tripp 1.1 339
Andy Tripp 4.1 340 {{velocity}}
Andy Tripp 1.1 341 #set($xwql = "from doc.object(FAQ.FAQClass) as faq where doc.fullName <> 'FAQ.FAQTemplate'")
342 #set($results = $services.query.xwql($xwql).execute())
343 #if($results.empty)
Andy Tripp 4.1 344   No FAQ has been created yet!
Andy Tripp 1.1 345 #else
Andy Tripp 4.1 346   #foreach ($item in $results)
347     #set($faq = $xwiki.getDocument($item))
348     * [[${faq.display("question").replace("<p>", "").replace("</p>", "")}>>${item}]]
349   #end
Andy Tripp 1.1 350 #end
Andy Tripp 4.1 351 {{/velocity}}
352 {{/code}}
Andy Tripp 1.1 353
Andy Tripp 4.1 354 * Copy this code and paste it as Wiki content inside ##FAQ.WebHome##
355 * Click "Save and View"
356 * New FAQ entries will now be displayed on the page once you create them
Andy Tripp 1.1 357
Andy Tripp 4.1 358 The ##FAQ.WebHome## page should look similar to this:
Andy Tripp 1.1 359
Andy Tripp 4.1 360 {{image reference="FAQsCustomCode.png"/}}
Andy Tripp 1.1 361
Andy Tripp 4.1 362 == Creating new FAQ entries ==
Andy Tripp 1.1 363
364 There are 2 ways for you to let your users create new FAQ entries:
365
Andy Tripp 4.1 366 1. Declare the FAQ as a template
367 1. Add a custom creation form
Andy Tripp 1.1 368
Andy Tripp 4.1 369 === Using a Template ===
Andy Tripp 1.1 370
Andy Tripp 4.1 371 You will have to define a template provider [[as explained on this page>>extensions:Extension.Administration Application||anchor="HCreatethetemplateprovider"]]
Andy Tripp 1.1 372
373 Go to your wiki's administration interface, in the "Templates" section (Administration -> Content -> Templates). Create a new template provider in the
Andy Tripp 4.1 374 ##FAQ## space and name it ##FAQTemplateProvider##
Andy Tripp 1.1 375
376 You can then use the following values:
377
Andy Tripp 4.1 378 * Provider name: ##FAQ Template Provider##
379 * Template name: ##New FAQ entry##
380 * Template to use: ##FAQ.FAQTemplate##
Andy Tripp 1.1 381
Andy Tripp 4.1 382 If you'd like, you can restrict FAQ entries so that they're created only in the ##FAQ## space. Once you're done, click "Save & View". Your template is now ready to be used! Users who click on the "Add >> Page" button will now have the option to create a new page using the FAQ template.
Andy Tripp 1.1 383
Andy Tripp 4.1 384 === Custom creation form ===
Andy Tripp 1.1 385
386 If you choose this option, you will need to write some code to let your users create new FAQ entries. To do this, you will have to create a form in which the user can enter the name of the questions she wants to create. Once typed in, the form calls the same page to trigger the new document creation based on the parameters entered by the user:
387
Andy Tripp 4.1 388 * The first part of the code checks whether the page has a parameter. If so:
389 ** The name of the document that will be created is computed
390 ** A check is done to verify the document doesn't exist yet
391 ** If everything's ok, the user is sent to the new document in inline edition mode
392 * The second part of the code is the actual FAQ creation form
393 ** It builds the name of the document to create it in the ##FAQ## space
394 ** It sets the document parent as being the current document
395 ** It defines the template to use to create the new document
Andy Tripp 1.1 396
Andy Tripp 4.1 397 {{code language="none"}}
398 {{velocity}}
Andy Tripp 1.1 399 #if("$!request.docName" != '')
Andy Tripp 4.1 400   ## Request for creating a new instance
401   #set($docName = ${request.docName})
402   #set($targetDocName = "${request.spaceName}.${docName}")
403   #if(!$xwiki.exists($targetDocName) && $xwiki.hasAccessLevel('edit', $xcontext.user, $targetDocName))
404     $response.sendRedirect($xwiki.getURL($targetDocName, 'inline', "template=${escapetool.url($request.template)}&parent=${escapetool.url($request.parent)}"))
405     ## Stop processing, since we already sent a redirect.
406     #stop
407   #end
Andy Tripp 1.1 408 #end
409
Andy Tripp 4.1 410 = Add a new question =
Andy Tripp 1.1 411
412 #if("$!targetDocName" != '' && $xwiki.exists($targetDocName))
Andy Tripp 4.1 413   {{warning}}The target document already exists. Please choose a different name, or [[view the existing document>>$targetDocName]]{{/warning}}
Andy Tripp 1.1 414 #elseif("$!targetDocName" != '')
Andy Tripp 4.1 415   {{warning}}You don't have permission to create that document{{/warning}}
Andy Tripp 1.1 416 #end
417
Andy Tripp 4.1 418 {{html}}
419   <form action="" id="newdoc" method="post">
420     <div>
421       <input type="hidden" name="parent" value="${doc.fullName}"/>
422       <input type="hidden" name="template" value="FAQ.FAQTemplate"/>
423       <input type="hidden" name="sheet" value="1"/>
424       <input type="hidden" name="spaceName" value="FAQ"/>
425       Document: <input type="text" name="docName" value="Enter your question here" class="withTip" size="50"/>
426       <span class="buttonwrapper"><input type="submit" value="Create this FAQ" class="button"/></span>
427     </div>
428   </form>
429 {{/html}}
430 {{/velocity}}
431 {{/code}}
Andy Tripp 1.1 432
Andy Tripp 4.1 433 * Copy this code and paste it as Wiki content inside ##FAQ.WebHome##, below the code that's already there
434 * Click "Save and View"
435 * A form to create new FAQ entries is now available on the page:(((
436 {{image reference="FAQsWithForm.png"/}}
437 )))
Andy Tripp 1.1 438
Andy Tripp 4.1 439 = Test the Application =
Andy Tripp 1.1 440
441 Now let's just create a new document in our application to test it out.
442
443 If you previously chose to use a "Custom creation form" for creating new FAQ entries, follow these steps:
444
Andy Tripp 4.1 445 * Go to ##FAQ.WebHome##
446 * Below the "Add a new question" header, enter a question (which will also be used as the document title) in the //Document// field
447 * Click //Create this FAQ//
448 * You can then enter your question in longer form using the //Question// field on the template, like this:(((
449 {{image reference="FAQSheetEdit.PNG"/}}
450 )))
451 * Click //Save & View// and then you will see the newly created document, like this:(((
452 {{image reference="FAQSheetView.PNG"/}}
453 )))
454 * Go back to the ##FAQ.WebHome## page (you can use the breadcrumbs) to see the list of existing questions(((
455 {{image reference="FAQsWithEntry.png"/}}
456 )))
Andy Tripp 1.1 457
Andy Tripp 4.1 458 = Conclusion =
Andy Tripp 1.1 459
460 This tutorial has taught you how to use the Class Wizard app and it has detailed the concepts of classes, objects and properties and introduced the authoring templates and page design sheets.
461
462 You may also have learned a little bit about Velocity scripting in documents. You can use these basic concepts to build custom applications at the document or presentation layer of XWiki without having to compile or deploy code.
463
464 As always, please take the time to make this document better for other users if you find ways that it can be improved as you read it for the first time.

Get Connected