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.php print empty '' string if value is 0 #2572

Open
technilogics opened this issue Feb 16, 2024 · 8 comments · May be fixed by #2617
Open

TemplateProcessor.php print empty '' string if value is 0 #2572

technilogics opened this issue Feb 16, 2024 · 8 comments · May be fixed by #2617

Comments

@technilogics
Copy link

return $subject ? Text::toUTF8($subject) : '';

return $subject ? Text::toUTF8($subject) : '';

Cause issue if subject is 0, that actually i want to print,
but unfortunately when $subject=0 as string or int or char it results in false.

Please fix this issue

@haihuynh98
Copy link

I also encountered a similar problem, and I haven't been able to solve it yet.

@truestealth
Copy link

my way to solve it:
override PHPWord/src/PhpWord/TemplateProcessor.php in composer.json
and replace
return $subject ? Text::toUTF8($subject) : ''; >>> return (null !== $subject) ? Text::toUTF8($subject) : '';

@jeremygmajor
Copy link

Thanks @truestealth - works great!

@cavasinf cavasinf linked a pull request May 30, 2024 that will close this issue
4 tasks
@adriansuter
Copy link

As a workaround, you could prepend the 0 with a zero width no-break space:

$templateProcessor->setValue('test', mb_chr(hexdec('FEFF')) . '0');

@pbloot
Copy link

pbloot commented Jul 8, 2024

I encountered a similar issue when trying to pass a string "0" to the template processor, and eventually fixed it by adding a zero width space character to the string, so it doesn't get evaluated as false-y thus reducing it to an empty string:

'0' . "\u{200B}"

@dsuurlant
Copy link

+1, we are running into this issue as well. Hope the fix gets merged soon. Thank you all for your workarounds.

@supernova-ws
Copy link

As a workaround, you could prepend the 0 with a zero width no-break space:

$templateProcessor->setValue('test', mb_chr(hexdec('FEFF')) . '0');

Thanks, works like a charm!

Bump for a fix

@wjycoder
Copy link

I encountered a similar issue when trying to pass a string "0" to the template processor, and eventually fixed it by adding a zero width space character to the string, so it doesn't get evaluated as false-y thus reducing it to an empty string:

'0' . "\u{200B}"

Thanks, it works, while "\u{200B}" displays as a space, "\u{200D}" looks better cause it is invisible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

9 participants