Replies: 4   Views: 1634
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  · 02-03-2020 - 14:24

I am trying to create two documents from similar templates (the template use the same variable names).

The call replaceVariableByText fails in multiple places on the second document. 

I have tried unset and = null before creating the second document. Neither change the behaviour.

There is nothing wrong with either template because whichever is creted first works fine it is always the replace in the second document that fails. 

I can't find and reset or clear function and the only reference in the forums is to using unset (which doesn't work)

Any help apprceiated.


Posted by admin  · 02-03-2020 - 15:12


We have run a script that uses ten different templates to generate several outputs, and in all cases, all placeholders are replaced correctly.

There's no need to unset or set to null the variables, CreateDocxFromTemplate resets the needed variables automatically.

Please send to contact[at] the most simple script that illustrates your issue (without doing external connections such as databases or webservices) and the templates you are using and we'll check them.


Posted by TimSmith  · 02-03-2020 - 15:32

Do the templates that you run use the same variable names? 

Mine is a simple loop over two template names (templates nearly identical), replacing the same variables in each loop. 

It's not very complicated. But it doesn't work. 


Posted by admin  · 02-03-2020 - 15:37


Yes, three templates have the same placeholder names but with different contents. We'd like to test the code you are running, please send it to contact[at] attaching both templates too.


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');
CreateDocxFromTemplate::$_preprocessed = true;


  • 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 = new CreateDocxFromTemplate('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));