Skip to content

TemplateProcessor setComplexBlock /Value and Sections #2561

Open
@oleibman

Description

@oleibman

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:

  • PHP Version: 8.1 (but probably any)
  • PHPWord Version: 1.2 (but probably any)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions