FAQTutorialManual

Version 1.1 by Vincent Massol on 2006/12/05

Building a FAQ Application

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. 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. In addition to the data model, this tutorial will introduce the concept of Authoring Templates and Page Design Sheets. Completing this tutorial is a recommended prerequisite for anyone who wants to build custom applications on the XWiki engine.

Application Overview

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.

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.)

  • The FAQs Summary View
  • A Question and Answer Page in Display Mode
  • A Question and Answer Page in Edit Mode

Authoring Templates and Page Design Sheets

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. Each form field can have different types of form elements. These form elements are defined by the properties of a class.

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. 

In this sample application, a new document is created and the object instance is automatically added to the new document. So, in truth, objects in XWiki are not really documents; they are added to a document. Once an object is added to a document, it's properties can easily be accessed by scripting in that document's content.

The Page Design Sheet is like a style sheet that defines each document will look when it is 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.

Get Set with the Class Editor Wizard

Five pages have been developed which collectively make up a Class Editor Wizard application that can 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 four pages of the wizard, you'll need to make the missing pages using code provided by this tutorial.

  • 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.
  • 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.
  • Navigate to the XWiki.XWikiClasses page.

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. If you have Administrative rights, you should see four links under the "Models" heading:

  • ClassSheet
  • ClassTemplate
  • ClassItemSheet
  • ClassItemTemplate

If any of the links are showing up with question marks, you do not have the page in your XWiki database. Copy the page code from the links below and paste it into the respective pages in your XWiki instance to ensure that you have all the parts of the wizard in tact. Once you have all of these pages in place, you are ready to start building your FAQs application.

Create the FAQ Class

  • On the Class Editor wizard entry page (XWiki.XWikiClasses), under the heading "Create a new Class", enter the following web space and class name:
    • Web: FAQs
    • Class: FAQ
  • Click the "Create this Class" button. You should then see a code page with the following code:
<!-- replace Main with the Space where you want your documents to be created -->
<!-- replace the default parent with the one of your choice -->
<!-- Save this template using the &#8217;Save&#8217; button -->
#\set( $\class = $\doc.name.substring(0,$\doc.name.indexOf("Class")))
#\set($\defaultparent = "XWiki.$\{class}Class")
#\set($\defaultweb = "Main")
#\include\Form("XWiki.ClassSheet")
  • 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:
#\set($\defaultweb = "FAQs")
  • Click the "Save" button. The class is now created and you should the be looking at a page titled "Class: FAQClass" that looks like this:

FAQClassDoc.gif

Add Properties to the Class

Under the page title, you should see the words "The first thing to do is to Edit the Class to add properties to it." So, let's just follow those instructions!

  • Click on the 'Edit the Class' link; a blank page will be displayed.
  • Note that the link trail in the header is something like "XWiki.XWikiClasses > FAQs.FAQClass". Even though the page is blank, this shows you are indeed on the class page.

In our document, we'll store both a question and an answer. So we need a property for each. 

  • Enter the text question in the Property Name field of the Class Editor panel.
  • 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.
  • Configure this property with the following values:

QuestionProperty.gif

  • Now add a property called answer in the same way that you did for the 'question' property (choosing TextArea for the property type).
  • Configure this property with the following values:

AnswerProperty.gif

  • When you are done adding and configuring the properties, click the 'Save Class' button.

Create the Page Design Sheet

  • Search for the keywords 'FAQs.FAQClass' and click on the document to open it in XWiki.
  • The title of the page should be "Class: FAQClass" and it should look like this:

FAQClassSheet.gif

  • 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 in read mode. You should see the following page code for the document sheet:
#\#\ you can modify this page to customize the presentation of your object
#\#\ at first you should keep the default presentation
#\#\ save this template using the save button at the top left

1\ Document $\doc.name

#\#\ Change class name to your class name
#\set($\class = $\doc.getObject("XWiki.MyClass").xWikiClass)

<table border="1" cellspacing="0" cellpadding="2">
#\foreach($prop in $class.properties)
<tr><td> *$\{prop.prettyName}* </td>
<td>$\doc.display($\prop.getName())</td>
</tr>
#\end
</table>
  • Change the class name XWiki.MyClass in the code to FAQs.FAQClass so that the line looks like this:
#\#\ Change class name to your class name
#\set($class = $doc.getObject("FAQs.FAQClass").xWikiClass)
  • Click 'Save'.

Create the Authoring Template

  • Search for the keywords 'FAQs.FAQClass' and click on the document to open it in XWiki again. The document should look like this:

PageDesignSheetCreated.gif

  • Notice that now, there is a link for the FAQClassSheet in place of the button that was previously there. 
  • Click on the 'Create the document template' button. The template will be created with the following page code:
#\#\ replace MyName with the real class name
#\#\ save this template using the save button at the top left
#\include\Form("XWiki.MyNameClassSheet")
  • As the comments instruct, change the words MyName with FAQ so that instead of having XWiki.MyNameClassSheet, you will have FAQs.FAQClassSheet. 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.
  • Click 'Save' to save the page object.
  • Now the saved page object (the authoring template) should show the page title "Document FAQClassTemplate". Now, we need to associate the class object with this page object to turn it into a true authoring template.
  • Click 'More Actions > Edit Objects'.
  • In the Object Editor panel, select FAQs.FAQClass from the drop-down and then click 'Add Object from this Class'. The Question and Answer form fields appear on the page.
  • Click 'Save Objects'. Congratulations; You just created an Authoring Template!
  • Click the FAQs.FAQClass link in the header breadcrumb trail. As you can see, you are almost done!

MakingProgress.gif

Test the Application

Now let's just create a new document in our application to test it out. 

  • Enter a document title in the Document field and click Create. For example, enter 'What is the meaning of life'. Don't use a question mark in the document name.
  • You can then enter your question in longer form using the question field on the template, like this:

FirstQuestion.gif

  • Click Save and then you will see the newly created document, like this:

FirstQuestion2.gif

  • Search for 'FAQ' to find the FAQClass document again and click to open it. 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:

Final.gif

Displaying the FAQ Questions as summary

If you want to display the FAQ question instead of the FAQ document name in the summary area, you can proceed as follow:

  • Edit the content of the FAQs.FAQClass page
  • Replace the #includeForm("XWiki.ClassSheet") with the content of the XWiki.ClassSheet (go to the XWiki.ClassSheet, edit the content and past it instead of the includeForm command
  • After the #foreach($item in $xwiki.searchDocuments($sql)) command add the following line
     #set ($faq=$xwiki.getDocument(${item})) and replace the 
[\$\{item}]

 with 

[\$\{faq.display("question")}>$\{item}]

Finally, link to the FAQs.FAQClass page

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 FAQs.FAQClass. For example:

*\ [\Frequently Asked Questions (FAQs) > FAQs.FAQClass]

Conclusion

This tutorial has shown how to use the Class Wizard app and it has introduced the concepts of classes, objects, properties, 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.

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.

Tags:
   

Get Connected