Forum


Replies: 2   Views: 471
Variable replacement in parse mode in text field added in a textbox
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 npoapplications  · 26-05-2023 - 08:55

Hi,

I have a minimal example which produces invalid xml in parse mode when a variable is used in a textfield:

$docx = new CreateDocxFromTemplate($docxPath);
$docx->setTemplateSymbol('##');
$docx->setParseMode(true);

$docx->replaceVariableByText([$key => $placeholderValue]);

Without parseMode the variable isn't detected at all.


This template used work without problems in PHPDocx 6.5, but after upgrading to 14 it produces this error:

DOMDocument::loadXML(): Opening and ending tag mismatch: shape line 1 and t in Entity, line: 1
---ERRFILE---
/var/www/html/library/phpdocx/Classes/Phpdocx/Utilities/XmlUtilities.php
---ERRLINE---
30

 

Posted by admin  · 26-05-2023 - 09:56

Hello,

Since phpdocx 11, that supports using ${  } to wrap placeholders, when using different symbols at the beginning and the end or a symbol greater than 1 byte character, a custom CreateDocxFromTemplate::$regExprVariableSymbols must be set.
In previous versions a single symbol could be used: $, #, @... Using symbols with more than one character was not recommended because MS Word may break them in several tags.

On the API page (https://www.phpdocx.com/api-documentation/templates/set-Word-template-placeholder-variable-symbol)  you can read more information about this.

The easiest approach is using a single symbol ($, |, @...) or the default ${ } to wrap placeholders; in these cases you don't need to do any change in CreateDocxFromTemplate::$regExprVariableSymbols.

In your case, as you are using ## to wrap placeholders, please test the following code:

$docx = new CreateDocxFromTemplate('document.docx');
CreateDocxFromTemplate::$regExprVariableSymbols = '##(.*)##';
$docx->setTemplateSymbol('##');

$docx->replaceVariableByText([$key => $placeholderValue]);

A more complex approach could also be used to fix broken ## symbols as MS Word may add:

$docx = new CreateDocxFromTemplate('document.docx');
CreateDocxFromTemplate::$regExprVariableSymbols = '#(.*)#(.*)#(.*)#';
$docx->setTemplateSymbol('##');

$docx->replaceVariableByText([$key => $placeholderValue]);

If you sent to contact[at]phpdocx your sample DOCX we'll check it and test with the parseMode option. 

On https://www.phpdocx.com/faqs/forum/default/topic/2172 you can read a post of another user with a similar request.

Regards.

Posted by npoapplications  · 26-05-2023 - 10:17

Thank you very much, it works without parseMode and your suggested regex.
I falsely understood the api documentation that setting regExprVariableSymbols wasn't neccessary when using identical symbols at beginning and end but apparently this is limited to a single identical symbol.

I will send the template to the contact address in case you want to check parse mode anyway.