Register Lost your password?
Full api documentation: full api documentation FREE version full api documentation PRO version

public function addTemplateVariable(mixed variable, string value, array settings)

Parameters

$variable

Variable to replace in header, footer or document content, or array of values

$value

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

$settings

Template settings. Values: ‘header’ => true if exists a header or footer in table.
Or content type: ‘html’, ‘rtf’, ‘docx’, ‘mht’, ‘image’
Variables in Word document must be $VARIABLE$. The variable is replaced with the assigned value.

Examples

Example 1 Version Pro


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');

Example 2 Version Pro


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');

    5 Responses

  • jolyon2000

    Have just run this code:

    require_once(’../library/phpdocx/classes/cCreateDocx.inc’);
    $objDocx = new cCreateDocx();
    $objDocx->fAddLink(’Link to Google’, ‘http://www.google.es’, ‘Arial’);
    $objDocx->fCreateDocx(’example_link.docx’);

    I have not received any errors to the screen, but no file can be found with the given name on my server.

    Please help!

  • Herschel

    I’m trying to input a relatively long description (few paragraphs) into a variable. It corrupts the docx file because word can’t open it. Replacing variables works find when it’s just a few words. I’ve tried using the addText method to add it to the docx but that doesn’t seem to work either, perhaps because it’s a template?

  • overcubes

    When I use a template, I can not use functions such as addHTML (), AddTable (), addText ().

    How could blend these two techniques. there is a chance to do the following:

    $var = addHTML();

    / * then replace them * /
    $docx -> addTemplateVariable (’VAR’, $ var);

    thank you very much

  • admin

    Hello,

    Please check included examples on phpdocx pro; you can use a method like this one:

    $docx->addTemplateVariable(’NAME’,
    David Hume
    Character is the result of a system of stereotyped principles.‘,
    ‘html’);

    Regards.

  • Andrej

    With php 5.2.8 zipArchive::open doesn’t work

    Adding CREATE, OVERWRITE flags in CreateDocx.inc line 2452 will fix this:
    if ($zipDocxTemplate->open($fileName, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)) {