Replies: 3   Views: 3504
Bugs in addtemplate function
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 Chris McLean  · 05-05-2011 - 15:18


We've encountered a number of bugs with the addTemplate ( functions:

We have test files that we can supply but the key issues are:
[list]When an array is added using addTemplateVariable (multiple lines) the variable name (ie $VARIABLE$ is not removed[/list]
[list]When a variable is used more than once in a template and replaced with an array only the first instance of the variable is replaced[/list]
[list]When a variable is used more than once in a table only the first instance is replaced[/list]
[list]When a variable is used more than once in a table the table rows can be repeated multiple times (in our case the row was repeated 13 times[/list]

All tests were conducted using clean word docs with the variables pasted in as plain text.

Could you please tell us if these bugs are known and whether they are being fixed?



Posted by yoramv  · 11-04-2013 - 12:12

I have to second that. I'm trying to fill a table using the addTemplateVariables and the results for some reason depends on the template document. About 90% of the time I will get a lot of lines in the table with the $variable$ repeating over multiple rows. Other times, just by adding a line after the table (in the template file) it will fill correctly.

Is there a way to make templates and tables work reliably?

Posted by r2363c  · 21-10-2013 - 13:16


It may be a bit late, but we encountered the same problem. While assigning a 1 element array to a template variable templates placeholder were repeated when in a table. The problem was still present in 3.2 and 3.3 version.

To overcome this first test if array count is greater than 1 (1 element). If only one element in  your array, just assign the string variable of the array ($yourarray[0]) to your variable :

$replacedata = explode("\n",$maybemultilinedata);

$docx->addTemplateVariable($var,(count($replacedata )>1) ? $replacedata :$replacedata [0]);


r2363c / altidev team/ jca