Forum


Replies: 2   Views: 1532
Consistent way to read list variables
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 bkidd  · 23-06-2020 - 12:06

I use replaceListVariable to populate list items.  Is there any consistent way to read list items from a template?  I'd like to loop through list items to find placeholder values.  I am assuming I need to use getDocxPathQueryInfo but can't quite get the right logic to read the text of the line item.  In some cases, there are text fragments that need to be concatenated before checking if a placeholder exists (e.g. $BULLET$).

Any suggestions are appreciated.  Thanks.

Posted by admin  · 23-06-2020 - 14:47

Hello,

UPDATE: phpdocx 13.5 added getTemplateVariablesType to get the type of template variables.

Using getDocxPathQueryInfo you can query by many content types, such as list type, for example using the file TemplateList.docx included in the package:

$docx = new CreateDocxFromTemplate('examples/files/TemplateList.docx');

$referenceNode = array(
    'type' => 'list',
);

$queryInfo = $docx->getDocxPathQueryInfo($referenceNode);

foreach ($queryInfo['elements'] as $element) {
    var_dump($element->textContent);
}

You can iterate the elements key and get each value using the textContent attribute, that removes the tags to get the value of each result and returns the value of each list paragraph (in a DOCX a list is a paragraph with some specific w:numPr and w:ilvl tags). Using this code you can iterate the elements to get only those strings that contain the symbols uses to wrap the placeholders ($ as default).

Regards.

Posted by bkidd  · 24-06-2020 - 13:32

Okay, thank you this is very helpful.  Yes, I'm aware fo the getTemplateVariables method but, as you noted, I didn't know the element of the placeholder and therefore woulnd't know which method I would need to call to set/iterate the value(s).

I will use the code sample you provided.  Thanks