Forum


Replies: 2   Views: 147
Consistent way to read list variables

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,

The getTemplateVariables method returns all variables from a DOCX. There's no way to kno if the placeholder is in a table, paragraph or list using this method (all these contents include a paragraph). 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