Forum


Replies: 2   Views: 33
Using gettemplatevariables with ##

Posted by websurfermedia  · 23-09-2022 - 02:05

Hi Guys,

We were running version 10.0 Premium ad just  updated to 13.0 Premium.

It seems the function getTemplateVariables just stopped working, same code, same docx template. Worked perfectly with version 10. Has something changed that would cause this? The following code snippet returns an empty array, previous retured the array of placeholders

$docx = new \Phpdocx\Create\CreateDocxFromTemplate(template.docx');
$docx->setTemplateSymbol('##'); // Placeholder identified with ## ... ##
dd($docx->getTemplateVariables());

Andrew

Posted by admin  · 23-09-2022 - 05:44

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}

that it's the easiest way.

As explained previously, using not unique simbols different to the default ${ } requires 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 a more generic selector:

CreateDocxFromTemplate::$regExprVariableSymbols = '#(.*)#(.*)#(.*)#';

On the following topics you can find a similar information using other symbols:

https://www.phpdocx.com/en/forum/default/topic/2160

https://www.phpdocx.com/en/forum/default/topic/2172

Although following the previous information your code will work correctly, you can also send a sample DOCX template to contact[at]phpdocx.com and we'll generate a sample script using it.

Regards.

Posted by websurfermedia  · 26-09-2022 - 01:57

Ah thanks, i see now. This was somethign introduced in a version since 10.0 I gather