Replies: 12   Views: 4349
Addtemplatevariable only creates first row of table
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 PeerBr  · 13-01-2014 - 16:00

I was struggling with the same problem, but took a long hard look at PHPDocx's sourcecode and came up with what I did wrong:

The tutorial states $docx->addTemplateVariable($aData'table'array('header' => false));

But it also has a template block called "table", which is confusing. Because I want the theoretical ability to have n tables, and remove them if necessary, I enclosed them with $BLOCK_table1$, $BLOCK_table2$ etc., you get the deal. I assumed that I needed to do specify the block the variable should go to. That was wrong - "table" may be what the block is called, but "table" is also a fixed param. Anything value but "list" or "table" for that param will cause your array of variables to be evaluated only once, replacing what is inside the first row, but not duplicating it. Now I don't know if PHPDocx is able to have more than one table, it may be. If you want to try it, make sure the first variable in it is unique, as this is used to duplicate the rows. To be safe, all variables should be unique, though.

The maintainers of PHPDocx should perhaps update the documentation and change the block titles to "BLOCK_mytable".

Hope this helps somebody.

PS: These "notepad" tricks posted above are a bit misleading. Docx files are zipped folders containing XML, and when you have invalid variables, you need to open your .docx using not Word, but a program like WinRAR to see the files contained. Look for "word\document.xml". You can edit this file using any text editor (I prefer Altova XML Spy), and search for "$" if that is your variable delimiter. If you find something like "$frodoBag", then a bunch of XML, then "ginsOfTheShire$", you can safely delete anything in between until re-joining the variable "$frodoBagginsOfTheShire$" again. To be safe, you should delete auto-correction (both grammar and spelling) and tracking changes in Word. These are responsible for chopping up your text and insert invisible divisions. To be safer, either fully copy your variables from a plain-text file instead of typing them, or type them in one go. If you need to correct them, delete them as a whole and re-type them.