Creating LibreOffice templates

Advantages of using Office and LibreOffice templates:

  • Can produce ODT files.
  • Less time spent creating the document.

Disadvantages compared with text templates:

  • Produce larger documents.
  • More difficulty maintaining the templates over time.

Preliminaries

Register the Metadata group definition at Administration > Metadata.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE property-groups PUBLIC "-//OpenKM//DTD Property Groups 3.10//EN"
                                 "http://www.openkm.com/dtd/property-groups-3.10.dtd">
<property-groups>
  <property-group label="Sample Template" name="okg:tpl">
    <input label="Name" name="okp:tpl.name"/>
    <input label="Birth Date" name="okp:tpl.birth_date" type="date" />
    <select label="Language" name="okp:tpl.language" type="simple">
      <option label="Java" value="java"/>
      <option label="Python" value="python"/>
      <option label="PHP" value="php" />
    </select>
  </property-group>
</property-groups>

In order to make a relation between the template form field and the property group, you have to name them following a naming convention: replace ":" and "." with "_". Here you can see the matching between both fields:

Metadata field nameTemplate field nameTemplate field value

okp:tpl.name

okp_tpl_name

${okp_tpl_name!}

okp:tpl.birth_date

okp_tpl_birth_date

${okp_tpl_birth_date!}

okp:tpl.language

okp_tpl_language

${okp_tpl_language!}

Creating the template

To create a template in OpenOffice.org, go to View > Toolbars > Form Controls.

A new dialog box will appear and you will be able to add form elements to the document. This dialog box contains elements such as labels, text boxes, check boxes, etc.

The field that will be replaced with the value of a passed variable can be specified using the standard FreeMarker expression notation by typing the variable name into the document as follows:

Hi ${name}!

However, mixing processing instructions and normal text into the document may become confusing and clutter the layout. So JODReports provides an alternative way of inserting fields. You can insert a visual field in OpenOffice.org Writer from the menu Insert / Fields / Other... (or just press Ctrl+F2), then click on the Functions tab and select Input field. Change the field Reference to JOOScript. In the following Content field, enter the variable name (e.g. "$name"). This field will typically be displayed by Writer as a grayed rectangle with the variable name, while moving the mouse over it will reveal the field reference.

This is the OpenOffice.org document used in this sample tpl.odt.

Read about OpenOffice.org templates at JODReports.

Date format

JODReports uses FreeMarker internally, so you can take advantage of FreeMarker's formatting features. For example, you can set the date format as follows:

${okp_tpl_birth_date?string("yyyy-MM-dd")}

If the date may be null, then you need to check this (in the case of ODT templates you should use #if between "[" and "]"):  

[#if okp_tpl_birth_date??]${okp_tpl_birth_date?string("yyyy-MM-dd")}[/#if]

Learn more about FreeMarker formatting at Built-in Reference.  

Dynamic tables

You can even use the template feature to generate dynamic tables. Let's see an example.

 

In the screenshot you can see a document with a table. The header has a colored background and is also bold. The script shows the options to generate the table headers.

This is the OpenOffice.org document used in this sample: dynamic_table.odt 

Learn more about this feature at:

Configuring sample

  • Upload the tpl.odt file to /okm:templates/documents
  • Register the metadata definition okg:tpl at Administration > Metadata 
  • Enable the metadata from Profiles at Administration > Profiles 
  • Add the metadata group okg:tpl to the tpl.odt file.