Skip to content

Changing Macro chars to {{ VAR }} doesn't work #2636

@xJuvi

Description

@xJuvi

Describe the bug and add attachments

I tried to change the macro chars to the following:
Opening: {{
Closing: }}
The syntax is acording to twig because i want to use the same variables. It is easier for our key users to understand templates.

I found this solution: #2412 (comment)
This doesn't work. So i tried to change the macro chars directly in the via compower downloaded source code. Nothing happens. The template processor can't handle {{ VAR }} - if i change this to any other combination it works.

Expected behavior

The Chars can be changed to {{ }}

Steps to reproduce

No code example required because i tried this also with changing the default variables in the TemplateProcessor.php.

PHPWord version(s) where the bug happened

1.2.0

PHP version(s) where the bug happened

8.0

Priority

  • I want to crowdfund the bug fix (with @algora-io) and fund a community developer.
    I want to pay the bug fix and fund a maintainer for that. (Contact @Progi1984)

Activity

added this to the 1.3.0 milestone on Aug 15, 2024
modified the milestones: 1.3.0, 1.3.1 on Aug 31, 2024
modified the milestones: 1.4.0, 1.5.0 on Jun 5, 2025
xJuvi

xJuvi commented on Jul 22, 2025

@xJuvi
Author

Hey there,
i tried the last days to find the issue here.

The issue happen as result of the method fixBrokenMacros in the TemplateProcessor. The current regex pattern are too static. They're check if the macro is opened by two chars and closed by one char.

protected function fixBrokenMacros($documentPart)
{
$brokenMacroOpeningChars = substr(self::$macroOpeningChars, 0, 1);
$endMacroOpeningChars = substr(self::$macroOpeningChars, 1);
$macroClosingChars = self::$macroClosingChars;
return preg_replace_callback(
'/\\' . $brokenMacroOpeningChars . '(?:\\' . $endMacroOpeningChars . '|[^{$]*\>\{)[^' . $macroClosingChars . '$]*\}/U',
function ($match) {
return strip_tags($match[0]);
},
$documentPart
);
}

It's not easy to solve the pattern. I tried many things. In my test envirenment is found a solution, but now, some of the existing UnitTests doesn't work anymore. With my solution you can use many different chars in combination.

Maybe the UnitTests are incomplete or wrong? I can't repdocue the behavior from the methods testFixBrokenMacrosWithCustomMacro and testFixBrokenMacros in my test environment...

The other thing is the method ensureMacroCompleted - there are also the number of chars hardcoded. This is easy to solve.

protected static function ensureMacroCompleted($macro)
{
if (substr($macro, 0, 2) !== self::$macroOpeningChars && substr($macro, -1) !== self::$macroClosingChars) {
$macro = self::$macroOpeningChars . $macro . self::$macroClosingChars;
}
return $macro;
}

This issue is also releated to #2412 and maybe many others.

How will we proceed, @Progi1984 ?

Kind regards
Hannes

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Progi1984@xJuvi

        Issue actions

          Changing Macro chars to {{ VAR }} doesn't work · Issue #2636 · PHPOffice/PHPWord