News

Replacing variables in a PHPdocx Template

  • Jun 06, 2011

You can create Word templates with placeholder variables that will be replace later by PHPDocX with real data.
For example, you can use the addTemplateVariable method as follows to replace variables by strings of text:


require_once '../../classes/CreateDocx.inc';

$docx = new CreateDocx();

$docx->addTemplate('../files/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');