I am trying to replace multiple variables in a docx that are not separated by a space or a line break with phpoffice/phpword using the TemplateProcessor class.
I am replacing my simple text variables with setValue(‘var1′,’value’) and some complex blocks containing tables with setComplexBlock(‘var2’, $table) (with $table, being an instance of PhpOfficePhpWordElementTable). If I separate my values with space or line break, everything works fine. Now if I write something like
${var1}${var2}${var3} in my template and I try to replace var2 with a complex block (table in this case), then all adjacent variables just disappear, the var1/var3 that were replaced with some text just get erased. It’s even more tricky, because now let’s say I put this in my template: {var1}${var2}${var3}${var4}, and I replace var2 and var4 with a complex block (table), then var1 and var3 are erased and I don’t get two arrays but only one that is a merge of var2 and var4.
For further informations:
- I’m not putting any space between variables because the docx has to be perfect after replacements, no unwanted line breaks/spaces
- The complex blocks replacements take place at the end of the script, which means my simple text variables are already replaced, and replacing the complex block at the beginning doesn’t solve the issue
My question is how to properly do this ?
An alternative could be adding line breaks or spaces between variables in my template and remove them if the variable is empty, but I found nothing on how to do this in their doc/on the web. Every bit of advice will be welcome !