Forum


Replies: 2   Views: 262
Recursively replace a variable in a template that is loaded from an external file
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by admin  · 25-02-2025 - 07:05

Hello,

Please note that template methods don't replace variables added in embedded documents.
To accomplish the requested task, you need to replace the variables in the document to be embedded and then embed it:

$docx = new \CreateDocxFromTemplate('nds2.docx');
$docx->setTemplateSymbol('[', ']');
$docx->replaceVariableByText(['nds_cert' => 'bbb '], ["firstMatch" => false]);
$docx->createDocx('nds2_upd.docx');

$docx = new \CreateDocxFromTemplate('nds.docx');
$docx->setTemplateSymbol('[', ']');
$docx->replaceVariableByExternalFile(['nds_cert' => 'nds2_upd.docx'], ['matchSource' => true]);
$docx->CreateDocx('nds3.docx');

Or use mergeDocxmergeDocxAt, or importContents methods to import the contents from the document as regular content instead of as an embedded document. Using these methods you can use template methods to replace variables after merging/importing the contents.

Regards.