Forum


Replies: 5   Views: 1054
Placeholders are split across xml nodes
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 admin  · 01-09-2021 - 07:07

Hello,

By default, phpdocx allows using a single symbol ($, #, |...) or ${ } to wrap placeholders.

Using a not unique placeholder distinct than ${ }, such as #{ }, {{ }}, [], [[]]... requires customizing the static variable CreateDocxFromTemplate::$regExprVariableSymbols. You can read this same information on the documentation page of the setTemplateSymbol method (https://www.phpdocx.com/api-documentation/templates/set-Word-template-placeholder-variable-symbol):

Different at the beginning and the end: ${VAR}. phpdocx requires using ${ } to wrap placeholders that don't use the same symbol at the beginning and the end, to use other symbols the public static variable CreateDocxFromTemplate::$regExprVariableSymbols must be customized.

CreateDocxFromTemplate::$regExprVariableSymbols is a public static variable with a regular expression that must match the symbols you want to use to wrap placeholders (as this is a regular expression, please note you need to escape protected characters in regular expressions such as [ ]).

We recommend using the default symbols to wrap placeholders:

  • A single symbol (1 byte character): $VAR$, #VAR#, |VAR|...
  • Or ${ }: ${VAR}

As explained previously, using not unique simbols different to the default ${ } require customizing CreateDocxFromTemplate::$regExprVariableSymbols, for example, the following code:

CreateDocxFromTemplate::$regExprVariableSymbols = '\[.*\]';
$docx->setTemplateSymbol('[', ']');

print_r($docx->getTemplateVariables());

sets [ ] as symbols to wrap placeholders. Please note that this custom code may require some minor adjustments to get the exact regular expression to be used in your templates, for example to exclude [ if you add it as other content in the document:

CreateDocxFromTemplate::$regExprVariableSymbols = '\[[^\[]*\]';

This custom regular expression is required to clean splitted placeholders across XML nodes with processTemplate (this method is called automatically by phpdocx) when not using the default settings (a single symbol or ${ } to wrap placeholders).

Regards.