Forum


Replies: 1   Views: 480
Replacevariablebyhtml using block parameter replaces the entire line of the variable
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 dipeshc  · 19-07-2021 - 08:25

Hello,

I facing the problem when I use replaceVariableByHTML using block parameter replaces the entire line of the variable.

e.g:

// my word file content (document.docx)
/*
Item name: My item
Item Description: $DESCRIPTION$
Price: 999
*/

// and my item description is
$description = 'The Item features a stylish design and <span style="color:orange">innovative</span> features so that you can enjoy using the mobile phone for all that you need.
<br><br>
Warranty card, User guide, Clear soft case, Screen protector are included';

$docx = new CreateDocxFromTemplate('document.docx');

$docx->replaceVariableByHTML('DESCRIPTION', 'block', $description, array('isFile' => false, 'downloadImages' => false));

$docx->createDocx('output');

Problem:

The above code replaces the entire line(Item description line) with the $description.

But I want to keep the Item Description: as well.

I know we can use inline in the second parameter of the replaceVariableByHTML function but I want to use block instead.

Incorrect output:

Item name: My item
The Item features a stylish design and <span style="color:orange">innovative</span> features so that you can enjoy using the mobile phone for all that you need.


Warranty card, User guide, Clear soft case, Screen protector
Price: 999

Expected output:

Item name: My item
Item Description: The Item features a stylish design and innovative features so that you can enjoy using the mobile phone for all that you need.


Warranty card, User guide, Clear soft case, Screen protector are included.
Price: 999

 

Posted by admin  · 19-07-2021 - 08:29

Hello,

On the API documentation page of the method (https://www.phpdocx.com/api-documentation/templates/replace-variable-html-Word-document) you can read about the available replacement types:

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.

A block replacement removes the paragraph content and add the new HTML (block and inline contents). An inline replacement keeps the existing contents in the paragraph of the placeholder but adds only the inline contents from the HTML. There's no replacement type to do an inline replacement keeping block contents from the HTML.

Regards.