PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.16k stars 2.68k forks source link

TemplateProcessor setComplexBlock /Value and Sections #2561

Open oleibman opened 5 months ago

oleibman commented 5 months ago

Describe the Bug

The TemplateProcessor methods setComplexBlock and setComplexValue fail when attempting to use them on Sections. It is unclear whether this is by design or just an oversight.

Steps to Reproduce

Please provide a code sample that reproduces the issue.

<?php
require __DIR__ . '/vendor/autoload.php';

use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Shared\Html;
use PhpOffice\PhpWord\TemplateProcessor;

$templateProcessor = new TemplateProcessor($filename); // file containing only the text ${test}
$html = '<p><span style="background-color: #ff0000;">BugTracker X</span> is ${facing1} an issue.</p>';
$section = new Section(0);
Html::addHtml($section, $html, false, false);
$templateProcessor->setComplexBlock('test', $section);
$facing1 = new TextRun();
$facing1->addText('facing', ['bold' => true]);
$templateProcessor->setComplexValue('facing1', $facing1);
$outputFile = 'anything';
$templateProcessor->saveAs($outputFile);

Expected Behavior

File saved in $outputFile with expected substitutions.

Current Behavior

What is the current behavior?

Fatal error: Uncaught Error: Class "PhpOffice\PhpWord\Writer\Word2007\Element\Section" not found in C:\git\PHPWord\src\PhpWord\TemplateProcessor.php on line 312

A possible solution, if one is warranted, is to use class Container when Section would otherwise be used on the offending lines in setComplexBlock and setComplexValue.

Context

Please fill in your environment information: