Replies: 2   Views: 470
How to use gettemplatevariables with more than one symbol for placeholders (not using default ${})
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 joeph  · 06-08-2021 - 12:45

I have set 

->setTemplateSymbol('[', ']')

This works fine when replacing the variables. However, 


returns an empty array.



Posted by admin  · 06-08-2021 - 13:48


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 (

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}

that it's the easiest way.

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

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


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 = '\[[^\[]*\]';


P.S. we have changed your username to hide your email

Posted by joeph  · 09-08-2021 - 14:33

Great, works nicely!