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

setMacroChars with [ and ] #2412

Open
sguyomard opened this issue Jun 16, 2023 · 2 comments
Open

setMacroChars with [ and ] #2412

sguyomard opened this issue Jun 16, 2023 · 2 comments

Comments

@sguyomard
Copy link

sguyomard commented Jun 16, 2023

when using setMaroChars with [ and ] as starting and closing char, the template processor doesn't replace maros
the word docx file was made with office365, not online, word installed on a laptop.

Steps to Reproduce

$templateProcessor = new TemplateProcessor($localWordFileName);
$templateProcessor->setMacroChars('[', ']');

in $this->tempDocumentMainPart, I have for example :
<w:rPr><w:sz w:val="16"/><w:szCs w:val="16"/></w:rPr><w:t xml:space="preserve">Nous avons bien réceptionné votre demande du </w:t></w:r><w:r w:rsidR="00790EED"><w:rPr><w:sz w:val="16"/><w:szCs w:val="16"/></w:rPr><w:t>{{</w:t></w:r><w:r w:rsidR="10F8EA5E" w:rsidRPr="22CC4AC1"><w:rPr><w:sz w:val="16"/><w:szCs w:val="16"/></w:rPr><w:t>dateSoumis</w:t></w:r><w:r w:rsidR="00790EED"><w:rPr><w:sz w:val="16"/><w:szCs w:val="16"/></w:rPr><w:t>}}</w:t></w:r><w:r w:rsidRPr="22CC4AC1"><w:rPr><w:sz w:val="16"/><w:szCs w:val="16"/></w:rPr><w:t xml:space="preserve"> et vous remercions pour la confiance que vous nous accordez : </w:t></w:r></w:p><w:p w14:paraId="0F17B8AC" w14:textId="77777777" w:rsidR="00380A60" w:rsidRDefault="033A9107" w:rsidP="002C51CC"><w:pPr><w:spacing w:after="38"/><w:ind w:left="567" w:hanging="10"/></w:pPr><w:r w:rsidRPr="22CC4AC1"><w:rPr><w:sz w:val="14"/>

can't find out how str_replace($search, $replace, $documentPartXML) could work in this ?

the same xml part without specific marcoChars :

w:rsidRDefault="033A9107" w:rsidP="002C51CC"><w:pPr><w:spacing w:after="5" w:line="265" w:lineRule="auto"/><w:ind w:left="567" w:hanging="10"/></w:pPr><w:r w:rsidRPr="22CC4AC1"><w:rPr><w:sz w:val="16"/><w:szCs w:val="16"/></w:rPr><w:t xml:space="preserve">Nous avons bien réceptionné votre demande du </w:t></w:r><w:r w:rsidR="00334903"><w:rPr><w:sz w:val="16"/><w:szCs w:val="16"/></w:rPr><w:t>${dateSoumis}</w:t></w:r><w:r w:rsidRPr="22CC4AC1"><w:rPr><w:sz w:val="16"/><w:szCs w:val="16"/></w:rPr><w:t xml:space="preserve"> et vous remercions pour la confiance que vous nous accordez : </w:t></w:r></w:p><w:p w14:paraId="0F17B8AC" w14:textId="77777777" w:rsidR="00380A60" w:rsidRDefault="033A9107" w:rsidP="002C51CC"><w:pPr><w:spacing w:after="

here the macro is clean

if I put a $ and a { , here a piece of templace.docx :
image

I get this result :

image

it seems there is a glinch in the matrix with ${

Expected Behavior

example : [client_name] replaced by client name

Current Behavior

nothing replaced, we got [client_name]
I tried diffrent patterns like {# and }, [#, #], etc nothing works, only ${ }

@gdevilbat
Copy link
Contributor

Same here,

this is because, fixBrokenMacros function is called before macro changed by setMacroChar

@thomasb88
Copy link

fixBrokenMacros is called on TemplateProcessor.php Class Instanciation (and so it's too late to change it after class instanciation)

But macroOpeningChars and macroClosingChars are static, so if you define
setMacroOpeningChars, setMacroClosingChars and setMacroChars as static functions, it could work doing

TemplateProcessor::setMacroChars('[', ']');
$templateProcessor = new TemplateProcessor($localWordFileName);

(it means in TemplateProcessor.php to replace

public function setMacroOpeningChars(string $macroOpeningChars): void
{
self::$macroOpeningChars = $macroOpeningChars;
}

public function setMacroClosingChars(string $macroClosingChars): void
{
    self::$macroClosingChars = $macroClosingChars;
}

public function setMacroChars(string $macroOpeningChars, string $macroClosingChars): void
{
    self::$macroOpeningChars = $macroOpeningChars;
    self::$macroClosingChars = $macroClosingChars;
}

by

public static function setMacroOpeningChars(string $macroOpeningChars): void
{
self::$macroOpeningChars = $macroOpeningChars;
}

public static function setMacroClosingChars(string $macroClosingChars): void
{
    self::$macroClosingChars = $macroClosingChars;
}

public static function setMacroChars(string $macroOpeningChars, string $macroClosingChars): void
{
    self::$macroOpeningChars = $macroOpeningChars;
    self::$macroClosingChars = $macroClosingChars;
}

)

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

No branches or pull requests

3 participants