I have HTML strings from WYSIWIG editor that I want to add to template using Template Processor. After trying many things, I come up with this workaround
$phpWord = new PhpWord();
$section = $phpWord->addSection();
Html::addHtml($section, $data['provision_current']);
$elements = $section->getElements();
$placeArray = [];
for($i = 0; $i<count($elements); $i++)
{
$placeArray[$i] = '${place'.$i.'}';
}
$placeholder = implode("<w:br/>", $placeArray);
$templateProc = new TemplateProcessor(storage_path('Template.docx'));
$templateProc->setValue('here', $placeholder);
$templateProc->saveAs($file);
$templateSecond = new TemplateProcessor($file);
for($j = 0; $j<count($elements); $j++)
{
$templateSecond->setComplexBlock($placeArray[$j], $elements[$j]);
}
$templateSecond->saveAs($file);
Basically, I make placeholder with the same number as elements in the section and saving it to a file. Afterwards, I load the file using second template processor and use setComplexBlock to add the elements.
Strangely, the file that I got only had the first element added. After several tests, I noticed that setComplexBlock and setComplexValue will only replace my first placeholder and then somehow remove the other placeholders. Please help me, where did I go wrong?