Wiki source code of FAQTutorialManual

Version 38.1 by Silvia Macovei on 2009/09/28

Hide last authors
Vincent Massol 1.11 1 1 Creating a FAQ Application
Vincent Massol 1.1 2
Anca Luca 14.1 3 This tutorial will show you how to build a Frequently Asked Questions (FAQs) Application much like the one you can find on the [FAQ page>xwiki:FAQ.WebHome]. This is a very simple application that makes use of XWiki's classes, properties, and objects. It also uses a technique that you may frequently use as the basis for several different kinds of applications.
Vincent Massol 1.1 4
Anca Luca 14.1 5 All through this tutorial you should refer to the [XWiki Data Model> DevGuide.DataModel] for information on XWiki's data model. You might also use the [XWiki Scripting Guide> 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.
6
Vincent Massol 1.1 7 1.1 Application Overview
8
9 The FAQ application allows users to post a question by entering the question into a simple form field and then submitting the form. The question then appears in a list along with all other questions that have been previously posted. Users can click on the questions to view both the question and answer in a consistently styled view. If the question has not yet been answered, any user can post an answer to the question by editing the page. In edit mode, the page will display a web form that is always consistent for every FAQ page.
10
11 Let us begin by taking a look at what we are going to build. The system has the following views. Click any view link below to preview a screenshot. (Note: Firefox users can middle click or click down on the mouse scroll-wheel to open the links in a new tab.)
12
Anca Luca 14.1 13 * {attach:The FAQs Summary View|FAQsSummary.png}
14 * {attach:A Question and Answer Page in Display Mode|FAQSheetView.png}
15 * {attach:A Question and Answer Page in Edit Mode|FAQSheetEdit.png}
Vincent Massol 1.1 16 1.1 Authoring Templates and Page Design Sheets
17
Anca Luca 14.1 18 An Authoring Template is a template for creating documents of a specific type. Unlike a regular content page in edit mode with one field for freeform editing, an Authoring Template presents a custom set of form fields for creating a document with specific type of data. These form elements are defined by the properties of a class.
Vincent Massol 1.1 19
Anca Luca 14.1 20 In object oriented programming, remember that a ~~class~~ is a template for an object. Using the analogy of a cookie cutter, the ~~class~~ is the ~~cookie cutter~~ and the ~~objects~~ are the actual ~~cookies~~. An Authoring Template provides one way to represent a class visually so that users can fill out a form to set unique properties (values in form fields). When the user submits the form, they are creating a unique ~~object~~ of the ~~class~~ type.
Vincent Massol 1.1 21
Anca Luca 14.1 22 Precisely, an Authoring Template is a prototype document used to create other specific instances of documents of the same type, along with a method of exposing the creation process to the user: the properties edit form. Remember that a XWiki Document can contain objects and this is the case of an authoring template: it is a XWiki Document with an empty object of a specific class, which is duplicated to create more and more documents based on that model, using the data inserted by the user in the editing form as specific values for the particular object instance in the current copy.
Vincent Massol 1.1 23
Silvia Macovei 31.6 24 The Page Design Sheet is like a style sheet that defines what each document will look like when rendered. Even though the unique object instances will have different values for their properties, they will always have a consistent presentation display because they will be rendered through the Page Design Sheet. The XWiki API available in scripting languages provides a mechanism that will help us use the same sheet for both editing and view modes. We will see how we can achieve this once we get to the FAQ Design Sheet section.
Vincent Massol 1.1 25
Silvia Macovei 31.6 26
Vincent Massol 1.1 27 1.1 Get Set with the Class Editor Wizard
28
Silvia Macovei 31.7 29 Five pages, which collectively make up a Class Editor Wizard application, have been developed to assist you in this process. Your first task is to see if you have these pages in your XWiki already. If so, you'll make a link to the wizard app so that you can always access it easily from an administrative page. If you do not have all five pages of the wizard, you'll need to make the missing pages using code provided by this tutorial.
Vincent Massol 1.1 30
31 * Enter a search query for the keyword "XWikiClasses". This should return a document called XWikiClasses in the XWiki space (i.e. XWiki.XWikiClasses). This is the first of the five pages that make up the wizard and it is the main entry page to the wizard application.
32 * Take a moment to bookmark this page or, even better, create a link to it from an administrative page so you can access it more easily in the future.
33 * Navigate to the XWiki.XWikiClasses page.
34
vmassol 1.2 35 Now, you just need to verify that you have the other four pages that make up the wizard app and create them if you do not. Check if the following classes are listed on the XWikiClasses page:
Vincent Massol 1.1 36
37 * ClassSheet
38 * ClassTemplate
39 * ClassItemSheet
40 * ClassItemTemplate
41
Silvia Macovei 31.7 42 If they're not showing up or if they are showing up, but with question marks, you do not have the pages in your XWiki database.
Anca Luca 14.1 43 You can either use this provided {attach:ClassWizard.xar} to import the documents in your wiki (see the [Import guide> AdminGuide.ImportExport#HImportingXWikipages] in the XWiki Administration Guide for details on this operation) or copy the page contents from the links below and paste them into the respective pages in your XWiki instance (create them as children of the XWikiClasses page) to ensure that you have all the parts that make up the wizard:
Vincent Massol 1.1 44
Anca Luca 14.1 45 * Get the [XWikiClasses>http://www.xwiki.org/xwiki/bin/view/XWiki/XWikiClasses?viewer=code&showlinenumbers=0] page code.
46 * Get the [ClassSheet>http://www.xwiki.org/xwiki/bin/view/XWiki/ClassSheet?viewer=code&showlinenumbers=0] page code
47 * Get the [ClassTemplate>http://www.xwiki.org/xwiki/bin/view/XWiki/ClassTemplate?viewer=code&showlinenumbers=0] page code.
48 * Get the [ClassItemSheet>http://www.xwiki.org/xwiki/bin/view/XWiki/ClassItemSheet?viewer=code&showlinenumbers=0] page code.
49 * Get the [ClassItemTemplate>http://www.xwiki.org/xwiki/bin/view/XWiki/ClassItemTemplate?viewer=code&showlinenumbers=0] page code.
Vincent Massol 1.1 50
Anca Luca 14.1 51 Once you have all of these pages in place, you are ready to start building your FAQs application.
kpc 1.14 52
Anca Luca 14.1 53 Keep in mind that you should always use the Wiki editing mode when editing scripts inside documents.
54
Silvia Macovei 31.7 55
Vincent Massol 1.1 56 1.1 Create the FAQ Class
57
Silvia Macovei 32.1 58 * On the Class Editor wizard entry page (XWiki.XWikiClasses), under the heading "Create a new data type", enter the following web space and class name:
59 ** Space: FAQs
Vincent Massol 1.1 60 ** Class: FAQ
Anca Luca 14.1 61
Silvia Macovei 32.1 62 {image:CreateANewClass.PNG}
Anca Luca 14.1 63
Vincent Massol 1.1 64 * Click the "Create this Class" button. You should then see a code page with the following code:
65
Vincent Massol 12.1 66 {code:none}
Anca Luca 14.1 67 ## replace Main with the Space where you want your documents to be created
68 ## replace the default parent with the one of your choice
69 ## Save this template using the ?Save? button
70 #set( $class = $doc.name.substring(0,$doc.name.indexOf("Class")))
Vincent Massol 12.1 71 #set($defaultparent = "XWiki.${class}Class")
72 #set($defaultweb = "Main")
73 #includeForm("XWiki.ClassSheet")
Anca Luca 14.1 74
Vincent Massol 1.1 75 {code}
76
Anca Luca 14.1 77 In the code, change the word "Main" with the name of the space where you want you FAQ pages to be created as the commented instructions in the page code suggest. Replace the word "Main" with the word "FAQs". The line of code should now look like this:
Vincent Massol 1.1 78
Vincent Massol 12.1 79 {code:none}
80 #set($defaultweb = "FAQs")
Anca Luca 14.1 81
Vincent Massol 1.1 82 {code}
83
Silvia Macovei 33.1 84 Click the "Save & View" button. The class is now created and you should be looking at a page titled "Class: FAQ" that looks like this:
Vincent Massol 1.1 85
Silvia Macovei 32.1 86 {image:FAQClass1.PNG}
Vincent Massol 1.1 87
Anca Luca 14.1 88
georgosn 28.1 89
georgosn 29.1 90
georgosn 30.1 91
georgosn 31.1 92
Silvia Macovei 32.1 93
Silvia Macovei 33.1 94
Vincent Massol 1.1 95 1.1 Add Properties to the Class
96
Silvia Macovei 31.8 97 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." So, let's just follow those instructions!
Vincent Massol 1.1 98
Silvia Macovei 31.9 99 * Click on the 'class editor' link; a blank page will be displayed.
100 * Note that the link trail in the header is something like "FAQ: XWiki Space » Data types » FAQClass". Even though the page is blank, this shows you are indeed on the class page.
Vincent Massol 1.1 101
Anca Luca 14.1 102 In our document, we'll store both a ~~question~~ and an ~~answer~~. So we need a property for each.
Vincent Massol 1.1 103
Anca Luca 14.1 104 * Enter the text ~~question~~ in the Property Name field of the Class Editor panel (in the column on the right).
Vincent Massol 1.1 105 * Choose a TextArea type for the property and then click the 'Add Property' button. The TextArea will ultimately give us a multi-line text field in our authoring template.
Silvia Macovei 34.1 106
107 {image:AddQuestionProperty.PNG}
108
Anca Luca 14.1 109 * Configure this property with the following values (actually, if you are using the current XWiki version, you don't need to change anything else but the Pretty name – all the rest are the default values):
Vincent Massol 1.1 110
Silvia Macovei 34.1 111 {image:QuestionProperty.PNG}
Vincent Massol 1.1 112
113 * Now add a property called ~~answer~~ in the same way that you did for the 'question' property (choosing TextArea for the property type).
Anca Luca 14.1 114 * Choose it from the property list on the left and configure this property with the same values as the "question" property, except for the ~~name~~ and ~~pretty name~~ which will, obviously, match the new property we're creating.
115 * When you are done adding and configuring the properties, click the 'Save & View' button.
Vincent Massol 1.1 116
117
Silvia Macovei 31.8 118
Silvia Macovei 31.9 119
Silvia Macovei 34.1 120
Vincent Massol 1.1 121 1.1 Create the Page Design Sheet
122
Anca Luca 14.1 123 * After the previous step you are now on the FAQClass page which should look like this:
Vincent Massol 1.1 124
Silvia Macovei 37.1 125 {image:FAQClass2.PNG}
Vincent Massol 1.1 126
Anca Luca 14.1 127 * Click the first button to create the document sheet (the Page Design Sheet). This sheet determines how your page objects will be rendered to the user. You should see the following page code for the document sheet:
Vincent Massol 1.1 128
Vincent Massol 12.1 129 {code:none}
Vincent Massol 1.1 130
Anca Luca 14.1 131 11. You can modify this page to customize the presentation of your object
132 11. at first you should keep the default presentation.
133 11. Save this template using the save button.
134
Vincent Massol 12.1 135 1 Document $doc.name
Vincent Massol 1.1 136
Vincent Massol 12.1 137 ## Change class name to your class name
138 #set($class = $doc.getObject("XWiki.MyClass").xWikiClass)
Anca Luca 14.1 139 #set($hasProps = false)
Vincent Massol 12.1 140 #foreach($prop in $class.properties)
Anca Luca 14.1 141 #if($velocityCount == 1)
142 #set($hasProps = true)
143 <dl>
144 #end
145 <dt> ${prop.prettyName} </dt>
146 <dd>$doc.display($prop.getName())</dd>
Vincent Massol 12.1 147 #end
Anca Luca 14.1 148 #if($hasProps)
149 </dl>
150 #end
Vincent Massol 1.1 151 {code}
152
Silvia Macovei 37.1 153 * Change the class name ~~XWiki.MyClass~~ in the code to ~~FAQ.FAQClass~~ so that the line looks like this:
Vincent Massol 1.1 154
Vincent Massol 12.1 155 {code:none}
156 ## Change class name to your class name
Silvia Macovei 37.1 157 #set($class = $doc.getObject("FAQ.FAQClass").xWikiClass)
Vincent Massol 1.1 158 {code}
159
Silvia Macovei 37.1 160 * Let's take a moment now and analyze the code: with the line we just modified we retrieve the FAQ.FAQClass from the wiki and then we iterate through all its properties and display their values for the current document in a definition list. As we mentioned, XWiki provides a mechanism that help us create sheets used for both view and edit mode: this is the display function used in the line: {code:none}<dd>$doc.display($prop.getName())</dd>{code} It detects the current mode (edit or view) and displays the property referenced by it's argument as the mode dictates: for view, it is the value of the property, for edit it is a form field that will allow the user to edit it. This way we can use a single Design Sheet for both displaying and editing our FAQs. See the [XWiki API reference > DevGuide.API] and [XWiki Scripting > DevGuide.Scripting] pages for more details.
Vincent Massol 1.1 161
Anca Luca 14.1 162 * Click 'Save & View'.
163
Vincent Massol 1.1 164 1.1 Create the Authoring Template
165
Silvia Macovei 37.1 166 * Search for the keywords 'FAQ.FAQClass' and click on the document to open it in XWiki again, or click the FAQClass in the breadcrumbs. The document should look like this:
Vincent Massol 1.1 167
Silvia Macovei 37.1 168 {image:FAQClass3.PNG}
Vincent Massol 1.1 169
Silvia Macovei 37.1 170 * Notice that now, there is a link for the FAQSheet in place of the button that was previously there.
Anca Luca 14.1 171 * Click on the 'Create the document template' button. The Authoring Template will be created with the following page code:
Vincent Massol 1.1 172
Vincent Massol 12.1 173 {code:none}
Anca Luca 14.1 174 ## Replace MyName with the real class name.
175 ## Save this template using the save button.
Silvia Macovei 37.1 176 #includeForm("XWiki.MyNameSheet")
Vincent Massol 1.1 177 {code}
178
Silvia Macovei 37.1 179 * As the comments instruct, change the words ~~MyName~~ with ~~FAQ~~ so that instead of having ~~XWiki.MyNameSheet~~, you will have ~~FAQ.FAQSheet~~. In this case, we changed the space preceding the page name also because we want all of our FAQ page objects to reside in a unique wiki web space.
Anca Luca 14.1 180 * Click 'Save & Continue' to save the document. Remember that all our documents will be copies of the Authoring Template used as a prototype so the content we just saved 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 > DevGuide.IncludeInVelocity] for more information regarding this technique.
181 * Now, we need to associate the prototype object with this document to turn it into a true authoring template.
182 * On the right side, in the Choose editor list, click ~~Objects~~.
Silvia Macovei 37.1 183 * In the Add Object panel on the right, select FAQ.FAQClass from the drop-down and then click 'Add Object from this Class'. The ~~Question~~ and ~~Answer~~ form fields appear on the page.
Anca Luca 14.1 184 * Click 'Save & View'. Congratulations; You just created an Authoring Template!
Silvia Macovei 37.1 185 * Click the FAQ.FAQClass link in the header breadcrumb trail. As you can see, you are almost done!
Vincent Massol 1.1 186
Silvia Macovei 38.1 187 {image:FAQClass5.PNG}
Vincent Massol 1.1 188
Silvia Macovei 38.1 189
Vincent Massol 1.1 190 1.1 Test the Application
191
192 Now let's just create a new document in our application to test it out.
193
Silvia Macovei 37.1 194 * Under the "Create a new document" header, enter a document title in the ~~Document~~ field and click ~~Create this document~~. For example, enter ~~What is the meaning of life?~~.
Anca Luca 14.1 195 * You can then enter your question in longer form using the ~~Question~~ field on the template, like this:
Vincent Massol 1.1 196
Silvia Macovei 37.1 197 {image:FAQSheetEdit.PNG}
Vincent Massol 1.1 198
Anca Luca 14.1 199 * Click ~~Save & View~~ and then you will see the newly created document, like this:
Vincent Massol 1.1 200
Silvia Macovei 37.1 201 {image:FAQSheetView.PNG}
Vincent Massol 1.1 202
Anca Luca 14.1 203 * Open the FAQClass document again. You can now see that the FAQ documents you are creating will build in a list on the main entry page to the application, like this:
Vincent Massol 1.1 204
Silvia Macovei 37.1 205 {image:FAQClass4.PNG}
Vincent Massol 1.1 206
207 1.1 Displaying the FAQ Questions as summary
208
Silvia Macovei 37.1 209 If you want to display the FAQ question instead of the FAQ document name in the summary area, you can proceed as follows:
210 * Edit the content of the FAQ.FAQClass page
Anca Luca 14.1 211 * Replace the #\include\Form("XWiki.ClassSheet") with the content of the XWiki.ClassSheet (go to the XWiki.ClassSheet, edit the content and paste it instead of the #\includeForm command
Silvia Macovei 37.1 212 * Then, after the {code:none}#foreach ($item in $xwiki.searchDocuments($sql)){code} command add the following line {code:none}#set ($faq=$xwiki.getDocument(${item})){code} and replace the {code:none}[$item]{code} with {code:none}[${faq.display("question")}>${item}]{code} The line we added retrieves the document using the document name while the line we replaced displays the "question" property for the document instead of the document name.
Anca Luca 14.1 213 * Click "Save & View" and enjoy your new FAQ application.
Vincent Massol 1.1 214
Anca Luca 14.1 215
Vincent Massol 1.1 216 1.1 Finally, link to the FAQs.FAQClass page
217
Silvia Macovei 37.1 218 We don't want our users to have to search for this page using the search engine, so finally pick a spot on your Wiki and make a user-friendly link to FAQ.FAQClass. For example:
Vincent Massol 1.1 219
Vincent Massol 12.1 220 {code:none}
Silvia Macovei 37.1 221 [Frequently Asked Questions (FAQs) > FAQ.FAQClass]
Vincent Massol 1.1 222 {code}
223
Anca Luca 14.1 224
jingbo 27.1 225
Vincent Massol 1.1 226 1.1 Conclusion
227
Anca Luca 14.1 228 This tutorial has shown how to use the Class Wizard app and it has detailed the concepts of classes, objects, properties and introduced the authoring templates and page design sheets. 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.
Vincent Massol 1.1 229
230 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