Forum


Replies: 4   Views: 1791
Second document from template replacevariablebytext fails
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 TimSmith  · 04-03-2020 - 09:42

After some absolutely unparallelled support (thanks) .. this is the answer (should anyone need it)

The problem comes from how placeholders and the preprocessed option are handled with clone blocks.

There're two easy approaches to solve the issue:

  • handle the preprocessed option after the template is loaded . We have added two lines to your code:

$docx = new CreateDocxFromTemplate('templateName.docx');
$docx->processTemplate($docx->getTemplateVariables());
CreateDocxFromTemplate::$_preprocessed = true;

$docx->replaceVariableByText($values);

  • generate optimize templates to work with them. We recommend you to use this approach, as it returns the best performance (you don't need to call processTemplate for each template when you load it, you can use the optimized templates directly). Instead of using not cleaned templates, you can generate optimize templates:

$docx = new CreateDocxFromTemplate('templateName1.docx');

$docx->processTemplate($docx->getTemplateVariables());

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

 

$docx = new CreateDocxFromTemplate('templateName2.docx');

$docx->processTemplate($docx->getTemplateVariables());

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

 

You just need to run the previous code one for each template.

Then you can use the new files (that are optimized) directly in your scripts instead of the old ones, setting the preprocessed option as true when loading them:

 

$templates = array(
  '
templateName1_optimized'', 'templateName2_optimized',
);

(...)

$docx = new CreateDocxFromTemplate($templates[$ii].'.docx', array('preprocessed' => true));

$docx->replaceVariableByText($values);