Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TemplateProcessor setComplexBlock /Value and Sections #2561

Open
oleibman opened this issue Jan 31, 2024 · 0 comments · May be fixed by #2562
Open

TemplateProcessor setComplexBlock /Value and Sections #2561

oleibman opened this issue Jan 31, 2024 · 0 comments · May be fixed by #2562

Comments

@oleibman
Copy link
Contributor

oleibman commented Jan 31, 2024

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)
oleibman added a commit to oleibman/PHPWord that referenced this issue Jan 31, 2024
Fix PHPOffice#2561. Allow the TemplateProcessor methods `setComplexBlock` and `setComplexValue` to work with Sections.
@oleibman oleibman linked a pull request Jan 31, 2024 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

1 participant