TinyButStrong - the PHP Template Engine

tbsOOo : TinyButStrong OOo

Author : Olivier LOYNET (tbsooo@free.fr)
Date : 2006-06-12
Version : 0.7.9
Web site : http://www.tinybutstrong.com
Forum : http://www.tinybutstrong.com/forum.php
Released under the GNU LGPL license : http://www.gnu.org/copyleft/lesser.html

tbsOOo PHP class extends the template engine TinyButStrong.

This class allows to create OpenOffice documents dynamically by separating display formatting from logic and data.
In practice, you create a template using OpenOffice with the TinyButStrong tags.
Then you create a PHP script that merges the template with a data source to get a new OpenOffice document.







server side

client side


Zip and Unzip

The Zip and Unzip binaries are required on server to extract/update XML files of OpenOffice documents with the class.
The default values in class for the binaries path are set to 'zip' and 'unzip'.
By the way, the server environment var 'path' could be set where the binary are installed to have the same setting for Linux and DOS environment and leave the default value.
On Linux, the typical path where ZIP and UNZIP are installed are '/usr/bin/zip' and '/usr/bin/unzip'.
On Win32, install ZIP and UNZIP from info-zip, by example to 'c:\program files\bin\zip.exe' and 'c:\program files\bin\unzip.exe' and add to the PATH environment var the following path: Set PATH=c:\program files\bin;%PATH%
Ask your administrator for assistance.

The process directory

The process directory is required to generate new OpenOffice documents. All the operations (extract, merge, ...) are made in this directory with a unique name.
This directory had to be writable for the PHP script (usually the Apache user).
The default value in class is set to 'tmp/' relative to the PHP scripts.

Otherwise you can use the 'tmp' system directory by example :


OpenOffice is required on client side to create templates and to visualize result after processing.
To know more on the OpenOffice file format, go to the tips section.


Step 1. Create a template with OpenOffice.

Step 2. Create a PHP script with the following code.


// data
$x = 'Hello World';

// instantiate a TBS OOo class
$OOo = new clsTinyButStrongOOo;

// setting the object
$OOo->SetDataCharset('ISO 8859-1');

// create a new openoffice document from the template with an unique id

// merge data with OOo file content.xml

// display
header('Content-type: '.$OOo->GetMimetypeDoc());
header('Content-Length: '.filesize($OOo->GetPathnameDoc()));

Step 3. Execute the script to get an new document with data merged !.


Before everything, install OpenOffice on your machine to visualize these examples.

  template result
invoice example show on RMLL 2005 example_invoice.sxw example_invoice.php

TBS examples converted to tbsOOo
  template result
Hello World tbsooo_us_examples_hello.sxw tbsooo_us_examples_hello.php
variable merge tbsooo_us_examples_var.sxw tbsooo_us_examples_var.php
system variable merge tbsooo_us_examples_system.sxw tbsooo_us_examples_system.php
array merge tbsooo_us_examples_dataarray.sxw tbsooo_us_examples_dataarray.php
array merge horizontal tbsooo_us_examples_datanum.sxc tbsooo_us_examples_datanum.php
TBS frm parameter use tbsooo_us_examples_prmfrm.sxw tbsooo_us_examples_prmfrm.php
TBS max parameter use tbsooo_us_examples_prmmax.sxw tbsooo_us_examples_prmmax.php
TBS headergrp parameter use tbsooo_us_examples_prmheader.sxw tbsooo_us_examples_prmheader.php
TBS block syntax tbsooo_us_examples_blocks.sxw tbsooo_us_examples_blocks.php
TBS sub-block syntax tbsooo_us_examples_subblock.sxw tbsooo_us_examples_subblock.php
TBS serial syntax tbsooo_us_examples_prmserial.sxw tbsooo_us_examples_prmserial.php
TBS conditionnal display tbsooo_us_examples_cond.sxw tbsooo_us_examples_cond.php
TBS event syntax tbsooo_us_examples_event.sxw tbsooo_us_examples_event.php
TBS loop syntax tbsooo_us_examples_loops.sxw tbsooo_us_examples_loops.php?nbr=10


Class definition



boolean SetZipBinary (string $path_binary[, $test=false]) Set the zip binary to compress XML files to OpenOffice documents
boolean SetUnzipBinary (string $path_binary [, $test=false]) Set the unzip binary to uncompress XML files from OpenOffice documents
boolean SetProcessDir (string $process_path) Set the folder to process temporary OpenOffice documents
void    SetDataCharset (string $charset) Fix the source data's charset (ISO 8859-1 or UTF8)
mixed   NewDocFromTpl (string $ooo_template_filename) Create an new OpenOffice document from the OpenOffice template
boolean LoadXmlFromDoc (string $xml_file) Unzip the XML file from the OpenOffice file and load as a TBS template to be merged
boolean SaveXmlToDoc (void) Save the result from TBS into the XML file and zip into the OpenOffice document
void    AddFileToDoc (string $filename) Add a file into the OpenOffice document
string  GetPathnameDoc (void) Get the path of the new OpenOffice document
string  GetMimetypeDoc (void) Get the document mime type
stream  FlushDoc (void) Flush the document to STDOUT
void    RemoveDoc (void) Unlink the document
void    ClearProcessDir ([int $hour=2 [, int $minut=0]]) Clean the process directory

Method: SetZipBinary

Method: SetUnzipBinary

Method: SetProcessDir

Method: SetDataCharset

Method: NewDocFromTpl

Method: LoadXmlFromDoc

Method: SaveXmlToDoc

Method: AddFileToDoc

Method: GetPathnameDoc

Method: GetMimetypeDoc

Method: FlushDoc

Method: RemoveDoc

Method: ClearProcessDir