Replies: 8   Views: 4479
Addtemplatevariable & 'table' with line breaks in table cells
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 falcana  · 08-01-2014 - 16:55

I would like to fill a table in a docx document by using addTemplateVariable with the 'table' paramenter.

Basically that works fine, but I have the problem, that the content for the cells contains line breaks "\n". I tried different ways, but I don't get the result to have the line breaks in the table cells in the docx. They are simply ignored, so all the text is in one line.


$data = array(
'a' => "Line1\nLine2",
'b' => "Line1\nLine2\nLine3",
'a' => "Line1\nLine2",
'b' => "Line1\nLine2\nLine3",

$docx->addTemplateVariable($data, 'table', array('header' => false));

Is there any way to get this done?

Regards, R.

Posted by jorgelj  · 09-01-2014 - 09:17


We recommend you to do that in two steps:

1. Replace the row with as many rows as you need using the addTemplateVariable method and adding new placeholders instead of the values.

2. Replace the new placeholders with the needed values.


Posted by falcana  · 14-01-2014 - 09:56

Thanks for the reply. Unfortunately your suggestion isn't an option for me. It's not predictable, how many line breaks the cell content will contain, as it is user generated content. The user can enter 2 or 20 lines or even more.

Isn't it possible to solve this somehow? You only need to consider the "\n" in the content and make a line break out of it in the docx.

I'm sure I'm not the only user facing this problem.


Posted by jorgelj  · 15-01-2014 - 09:33


We have added a task with high priority to add that feature.


Posted by falcana  · 17-01-2014 - 07:13

Many thanks, that's great news. Can you estimate, when it will be available?

Regards, R.

Posted by jorgelj  · 21-01-2014 - 10:13


Sorry but we don't have a release date for that feature. But it will be added as soon as possible.


Posted by robgeorgeuk  · 21-01-2014 - 17:48

+1, I have the same issue as @falcana and I agree an urgent fix is needed.

I have nearly managed to workaround the issue with nl2br and replaceTemplateVariableByHTML() but I get a space at the beginning of each line.



Posted by admin-phpdocx  · 11-02-2014 - 15:39

Although the parsing of line breaks is in the "pipeline" for next version you may include a paragraph in a table with addText and line breaks with no problem.


Please, see the API documentation of the addText method and how to include arbitrary WordML fragment in a table (addTable).

Best regards