Forum


Replies: 4   Views: 1484
No linebreak after <p> tag using replacevariablebyhtml
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-blocked  · 04-12-2020 - 10:36

Hello,

We switched from phpdocx4 to phpdocx11 and now with replaceVariableByHTML there is no line break after the closing </p> tag. The contents of the <p></p> tags are always lined up one after the other.

We have set the option

'strictWordStyles' => true

and I have also tried the settings

'isFile' => false

and

'parseDivsAsPs' => true

as described in the following article 
https://www.phpdocx.com/en/forum/default/topic/681
We already tried the help described there, tidy is activated.

The final call:

$docx->replaceVariableByHtml($var_name, 'inline', $value, $html_options);

Can we fix this with another configuration or is this a bug?

Thanks for any help!

Posted by admin  · 04-12-2020 - 11:14

Hello,

When an inline replacement type is done, block contents are cleaned from the new content (such as paragraphs), and only inline contents are added. To keep block contents from the content to be added when doing the replacements you need to set block as replacement type:

$docx->replaceVariableByHtml($var_name, 'block', $value, $html_options);

On the API documentation page of the method you can find the following information:

type

The substitution type, it can be:

"block": the variable, together with its containing paragraph, is replaced by the HTML content.
"inline": only the variable is replaced by the inline HTML content.

These options and how they work are the same than previous versions.

About the options you have posted they don't handle how to contents are replaced but other options: strictWordStyles (avoid some adding CSS styles, to be used with MS Word custom styles), isFile (to be used if the HTML is a file, not a string), parseDivsAsPs (change default div tags handling). On the previous API page these and other options are detailed.

Please note the linked topic in your post is about an extra space at the beginning of the paragraph, not about replacing placeholders

Regards.

Posted by falcana-blocked  · 09-12-2020 - 13:26

Thank you for your fast response. We checked that and using

block

instead of

inline

displays the string as expected. It's still not completely clear why this isn't working anymore now because with phpdocx4 it was also working with inline. I guess that was a bug then? It would be nice if there is a way to get the new lines to work with inline again because using block brings some other problems (e.g. other variables will be removed if one variable in this line gets removed) that we need to handle then.

Posted by admin  · 09-12-2020 - 14:20

Hello,

There's no known bug in phpdocx. That restriction was added some years ago to avoid generating corrupted DOCX when adding block contents inside other block contents. There's no option in the current release (and previous versions but very old ones that miss styles and may return corrupted documents) to do an inline replacement keeping block contents and styles.

If you want to use the same behaviour than the old phpdocx 4 you can edit CreateDocxFromTemplate.php, go to replaceVariableByHTML method and comment the following line:

$options['type'] = $type;

Commenting the previous line you'll get the same output than phpdocx 4 when doing inline replacements with HTML, but please note that corrupted DOCX and styles missing may happen so we don't recommended that change. A future release of phpdocx will allow adding block tags when doing inline replacements, but it's a work in progress.

Regards.

Posted by falcana-blocked  · 14-12-2020 - 08:34

Hello,

commenting out the line brings the desired behavior as before. Thanks for the quick and good help.

Regards.