Skip to content

TemplateProcessor, some values are not replaced (incl. debug) #548

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

Closed
kaystrobach opened this issue Jun 9, 2015 · 2 comments
Closed

TemplateProcessor, some values are not replaced (incl. debug) #548

kaystrobach opened this issue Jun 9, 2015 · 2 comments

Comments

@kaystrobach
Copy link

Given you have word document, with placeholders like ${some.object.path} any change in any place of the document can add some xml inside the brackets of the placeholder.

$template = new \PhpOffice\PhpWord\TemplateProcessor($this->templatePath);
throw  new \Exception(print_r($template->getVariables(), TRUE));

gives f.e. the following array

[0] => student.aktenzeichen
[1] => student.registraturSba
[2] => student.kontakt.vorname
[3] => st</w:t></w:r><w:r><w:t>u</w:t></w:r><w:r><w:t>dent.kontakt.nachname
[4] => student.kontakt.strasse
[5] => st</w:t></w:r><w:r><w:t>u</w:t></w:r><w:r><w:t>dent.kontakt.hausnummer
[6] => student.kontakt.plz
[7] => student.kontakt.ort
[8] => student.kontakt.nachname

3 and 5 will never be replaced as they are screwed up by word 😢 as i use getVariables to obtain real object pathes from my domain model i used strip_tags to transform it into a useable object path again.

But neither giving the broken values 3 / 5 to set values gives a successfull replacement ... any idea? my current approach is to digg deeper into the setValueForPart function and understand what you do with value cleaning ...

@kaystrobach
Copy link
Author

the following workaround makes the replacement a lot more robust for me!

current workaround:

        $variables = $template->getVariables();

        foreach ($variables as $variable) {
            if (strpos($variable, '.')) {
                list($object, $path) = explode('.', strip_tags($variable), 2);
                if (isset($this->variables[$object])) {
                    $value = $this->resolve($this->variables[$object], $path);
                    $template->setValue($variable, $value);
                    $template->setValue(strip_tags($variable), $value);

                    $this->logger->log('WordView Variable: ' . $variable . ' = ' . $value, LOG_DEBUG);
                }
            }
        }

        $this->logger->log('WordView leftovers', LOG_DEBUG, print_r($template->getVariables(), TRUE));

mean i do 2 replacements per document, one with the xml tags included and one with the xml removed with strip tags ... this makes it a little more robust for me, IMHO this should be fixed with some kind of mapping table, as the correct areas are detected.

d-damien pushed a commit to Novalian-Fr/PHPWord that referenced this issue Jan 15, 2016
@ghost ghost self-assigned this Apr 9, 2016
@ghost ghost added this to the 0.13.0 milestone Apr 9, 2016
ghost pushed a commit that referenced this issue Apr 9, 2016
@ghost
Copy link

ghost commented Apr 9, 2016

Fixed.

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

No branches or pull requests

2 participants