1

I'm using using PhpOffice library. Is there any option how to replace Variable in docx file with ordered list?

With templateProcessor method setComplexValue I can add text with code

$title = new \PhpOffice\PhpWord\Element\TextRun();
$title->addText('This title has been set ', array('bold' => true, 'italic' => true, 'color' => 'blue'));
$title->addText('dynamically', array('bold' => true, 'italic' => true, 'color' => 'red', 'underline' => 'single'));
$templateProcessor->setComplexBlock('title', $title);

So I tried this code for new List but it's not working.

$list = new \PhpOffice\PhpWord\Element\ListItem();
$list->addListItem('List item 1');
$list->addListItem('List item 2');
$templateProcessor->setComplexValue('list', $list);

Any Ideas? Thank you

Duracell
  • 11
  • 1
  • 3

3 Answers3

0

I did not find solution for this too and I have done it with text and line break. This solution is ok for my needs. Hope somebody share solution for list too.

$text = new \PhpOffice\PhpWord\Element\TextRun();
$text->addText('* List item 1');
$text->addTextBreak(1);                
$text->addText('* List item 2');
$text->addTextBreak(1);
$templateProcessor->setComplexValue('list', $list);
0

maybe someone'll find this useful.

Doc file (template) should be:

    ${block}
        ${item} <---- in MS Word, make this line a List Item
    ${/block}

PHP should be something like this:

    for($i=0;$i<count($YOUR_LIST_ITEMS);$i++) {
        $replacements[]= array('item' => $YOUR_LIST_ITEMS[$i]);
    }
    $templateProcessor->cloneBlock('block', 0, true, false, $replacements);
-1

You should use setComplexBlock instead

robBinlzX
  • 11
  • 1