Bilal H
Bilal H

Reputation: 33

PhpDocx Gettemplatevariables not working as expexted

I am using phpdocx-premium-12.5-ns. I created a document with variables like %%variable%%, then I try the below code:

 require_once 'classes/CreateDocx.php';

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

 $docx->setTemplateSymbol('%%', '%%');

 print_r($docx->getTemplateVariables());          

But it does not return anything, if I add a variable like ${variable} it is returned as ${variable}

If I set the symbol $docx->setTemplateSymbol('${', '}') it works normally and returns the variable.

My question: how can I use this variable %%variable%% and use the %% as a template symbol to get all the existing variables?

As I checked in the code of 'CreateDocxFromTemplate.php' class they call extractVariablesDistinctSymbols where they preg_match this pattern only: \$(?:\{|[^{$]*\>\{)[^}$]*\}

Upvotes: 0

Views: 178

Answers (1)

Bilal H
Bilal H

Reputation: 33

i get the answer from phpdocx

The character or symbol used to wrap template variables may be:

The same at the beginning and the end (setting only $templateSymbolStart): $VAR$, #VAR#... A single 1 byte character.
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.

If your template is using a symbol to wrap placeholders with more than 1 byte character, as it's the case of %%, or different at the beginning and the end you need to customize the public static variable CreateDocxFromTemplate::$regExprVariableSymbols. A simple sample:

CreateDocxFromTemplate::$regExprVariableSymbols = '%(.*)%(.*)%(.*)%';
$docx->setTemplateSymbol('%%');

We recommend using a single 1 byte character or ${} to wrap placeholders, so you don't need to generate a custom CreateDocxFromTemplate::$regExprVariableSymbols. This uses regular expressions, so when using protected characters they must be escaped.

Upvotes: 0

Related Questions