addTemplateVariable

Replaces a template variable by different types of content.

PRO, PRO+ and CORPORATE PHPDocX >= 1.7

Description

public addTemplateVariable (mixed $variable, string $data [, array $settings])

This method allows for:

  • the substitution of a template variable by text preserving the Word styles,
  • the substitution of a template variable by an image/docx/rtf/mht/html file,
  • the substitution of a series of variables within a Word table row by a series of rows with external data and
  • the substitution of a variable within a Word list by a list of items.

The variables to be replaced can be found in the main document or in the header and/or footer.

Remember that the PHPDocX template variable should be surrounded by $ symbols or whatever symbol we have chosen via the setTemplateSymbol method.

IMPORTANT: If the $settings parameter is set to 'html', 'docx', 'rtf', 'mht' or 'image', i.e. you are replacing the placeholder variable by a file, you may experience problems visualizing the resulting document in OpenOffice, LibreOffice or Word 2003 with the Compatibility Pack. If this is important for your particular project you may use any of the multiple alternative options available in the PHPDocX Template and DocXUtilities packages.

Parameters

variable

This parameter can be:

  • Simply the name of the variable to be replaced or
  • an array with the names of the variables and theirs corresponding values for "iterative substitutions" within tables and lists.

data

The value of this parameter may depend of the kind of sustitution we want to carry out:

  • a simple string of text,
  • the path to a file,
  • 'table' if we have used as first parameter an array of data for the generation of rows within a table or
  • 'list' if we used as first parameter an array for the generation of list items.

To replace the previous variable or file path or type of template (list or table).

settings

The posible keys and values are:

keyTypeDescription
header bool If we are populating data ina table set this parameter to true if the table has a header row.
content string If you are replacing the template variable by a file you should indicate its type: ‘html’, ‘rtf’, ‘docx’, ‘mht’ or ‘image’.

Return values

Void.

Code samples

Example #1: populates variables "one by one"


require_once 'classes/CreateDocx.inc';

$docx = new CreateDocx();

$docx->addTemplate('TemplateText.docx');

$docx->addTemplateVariable('WEIGHT1', '10');
$docx->addTemplateVariable('WEIGHT2', '20');
$docx->addTemplateVariable('WEIGHT3', '25');

$docx->addTemplateVariable('PRICE1', '5');
$docx->addTemplateVariable('PRICE2', '30');
$docx->addTemplateVariable('PRICE3', '7');

$docx->addTemplateVariable('TOTALWEIGHT', '55');

$docx->addTemplateVariable('TOTALPRICE', '42');

$docx->addTemplateVariable('NAME', 'David Hume');

$docx->createDocx('template_text.docx');

The resulting Word document looks like (download .docx file):

Example #2: uses an array to populate the values of a table


require_once 'classes/CreateDocx.inc';

$docx = new CreateDocx();

$docx->addTemplate('TemplateTable.docx');

$settings = array(
    'header' => true
);

$docx->addTemplateVariable(
    array(
        array(
            'NAME' => 'Product A',
            'WEIGHT' => '10',
            'PRICE' => '5'
        ),
        array(
            'NAME' => 'Product B',
            'WEIGHT' => '20',
            'PRICE' => '30'
        ),
        array(
            'NAME' => 'Product C',
            'WEIGHT' => '25',
            'PRICE' => '7'
        ),
    ),
    'table',
    $settings
);

$docx->addTemplateVariable('TOTALWEIGHT', '55');

$docx->addTemplateVariable('TOTALPRICE', '42');

$docx->addTemplateVariable('MYNAME', 'David Hume');

$docx->createDocx('template_table.docx');

The resulting Word document looks like (download .docx file):

Change log

  • Available since 1.0