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

New line with templateProcessor #1322

Open
1 of 3 tasks
R-Benjamin opened this issue Mar 23, 2018 · 24 comments
Open
1 of 3 tasks

New line with templateProcessor #1322

R-Benjamin opened this issue Mar 23, 2018 · 24 comments

Comments

@R-Benjamin
Copy link

R-Benjamin commented Mar 23, 2018

This is:

Expected Behavior

$myString = "Test-X \n Test-Y"
`Test-X'
'Test-Y'

Current Behavior

$myString = "Test-X \n Test-Y"
`Test-XTest-Y'

#838 <w:br/>\n and </w:t><w:br/><w:t> don't work. the problem is the same.

Failure Information

When I use the template processor, I can't insert a new line with a string.

How to Reproduce.

<?php
$templateProcessor->setValue('txt_agent_detail_evalB#'.$i.'', "test1 \n test2");
?>

Context

  • PHP version: 7
  • PHPWord version: 0.14
@lon9man
Copy link

lon9man commented Apr 20, 2018

+1

@R-Benjamin
Copy link
Author

Hi,

Any news about it ?

Thanks

@maba4891
Copy link

+1
Also the </w:t><w:br/><w:t> solution doesn't seem to work with
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
since the tags are escaped then.

@ozilion
Copy link
Contributor

ozilion commented Sep 12, 2018

+1

@Hydro8
Copy link

Hydro8 commented Sep 17, 2018

+1, any news ?

@maba4891
Copy link

Did anyone came up with a fix or workaround for this already?

@maba4891
Copy link

maba4891 commented Oct 26, 2018

Since I need this urgently I came up with a dirty workaround for this problem.

As I see it the exact problem is the following:

  1. I want to add newlines to stuff I add via TemplateProcessor (which works by adding </w:t><w:br/><w:t>).
  2. But I also want to use outputEscapingEnabled = true in phpword.ini so a simple & sign in the input doesn't break my word file.

Yet, output escaping uses htmlspecialchars and therefore also escapes and breaks the newlines.

I tried to fix this problem in Phpword/Escaper/Xml.php:

There I changed the function escapeSingleValue
FROM:

    protected function escapeSingleValue($input)
    {
        // todo: omit encoding parameter after migration onto PHP 5.4
        return htmlspecialchars($input, ENT_QUOTES, 'UTF-8');
    }

TO:

    protected function escapeSingleValue($input)
    {
        $escaped = htmlspecialchars($input, ENT_QUOTES, 'UTF-8');

        // we don't want to escape the newline code, so we replace it with html tag again
        $escaped_but_newlines_allowed = str_replace('&lt;/w:t&gt;&lt;w:br/&gt;&lt;w:t&gt;', '</w:t><w:br/><w:t>', $escaped);
        return $escaped_but_newlines_allowed;
    }

So after the input is escaped with htmlspecialchars I simply revert these changes for the newlines. Not very elegant but does the job for now.

@ghost
Copy link

ghost commented Mar 4, 2019

Any news on this issue?

@simogeo
Copy link

simogeo commented Jun 12, 2019

I'm also interested by having a cleaner solution ! Some news ?

@xavenix
Copy link

xavenix commented Jun 26, 2019

Same issue
Any news?

+1
Also the </w:t><w:br/><w:t> solution doesn't seem to work with
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
since the tags are escaped then.

@victor-ponamariov
Copy link

+1

@gautiermichelin
Copy link

help please, +1

@lyt8384
Copy link

lyt8384 commented Apr 17, 2020

+1

1 similar comment
@ozilion
Copy link
Contributor

ozilion commented Apr 17, 2020

+1

@damienlaguerre
Copy link

Here is my solution:

$templateProcessor->setValue('foo', 'Lorem.${newline}Ipsum....');

$new_line = new \PhpOffice\PhpWord\Element\PreserveText('</w:t><w:br/><w:t>');

$templateProcessor->setComplexValue('newline', $new_line);

I hope it helps.

@mukiraz
Copy link

mukiraz commented Oct 17, 2020

I added </w:t><w:p/><w:t> for a new paragraph and it is working well.

Here is the templatewords and printscreen of the word document:

${alternatif_block}
Alternatif-${altId}
${alternatif}
${/alternatif_block}

image

Here is the codes:

		$replacements = array(
		    array('altId' => '1', 'alternatif' => 'Lorem </w:t><w:p/><w:t> ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
'),			   
		);		

		$templateProcessor->cloneBlock('alternatif_block', 0, true, false, $replacements);

And here is the printscreen of output:

image

@mukiraz
Copy link

mukiraz commented Oct 17, 2020

I added </w:t><w:p/><w:t> for a new paragraph and it is working well.

Here is the templatewords and printscreen of the word document:

${alternatif_block}
Alternatif-${altId}
${alternatif}
${/alternatif_block}

image

Here is the codes:

		$replacements = array(
		    array('altId' => '1', 'alternatif' => 'Lorem </w:t><w:p/><w:t> ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
'),			   
		);		

		$templateProcessor->cloneBlock('alternatif_block', 0, true, false, $replacements);

And here is the printscreen of output:

image

But it works for only at first paragraph...

@Gadeoli
Copy link

Gadeoli commented Feb 22, 2021

Here is my solution:

$templateProcessor->setValue('foo', 'Lorem.${newline}Ipsum....');

$new_line = new \PhpOffice\PhpWord\Element\PreserveText('</w:t><w:br/><w:t>');

$templateProcessor->setComplexValue('newline', $new_line);

I hope it helps.

For you this code works in all breaks or for just the first?

@damienlaguerre
Copy link

It replaces all ${newline} not only the first one.

@Gadeoli
Copy link

Gadeoli commented Feb 22, 2021

@damienlaguerre tks for the quickly reply. I'll look why for me replace just the first.

@rimi-itk
Copy link

rimi-itk commented Jan 3, 2022

Unless I'm missing something, it seems that TemplateProcessor::setComplexValue() (cf. https://github.com/PHPOffice/PHPWord/blob/0.18.2/src/PhpWord/TemplateProcessor.php#L267-L293) only replaces one (the first) occurrence whereas TemplateProcessor::setValue() replaces all occurrences.

However, this is not documented clearly in neither https://phpword.readthedocs.io/en/latest/templates-processing.html#setcomplexvalue nor https://phpword.readthedocs.io/en/latest/templates-processing.html#setvalue, and therefore it's not obvious if this is by design or a missing feature (or even a bug). @troosan, can you comment on this?

As a (temporary) workaround, I'm using something along the lines of

$newline = new PreserveText('</w:t><w:br/><w:t>');
while (isset($templateProcessor->getVariableCount()['newline'])) {
  $templateProcessor->setComplexValue('newline', $newline);
}

to replace ${newline} until all occurrences are replaced.

@rimi-itk
Copy link

rimi-itk commented Jan 3, 2022

#2038 has been labeled “WontFix”.

@zhouyixiang
Copy link

zhouyixiang commented May 6, 2022

I'm not sure why the solution using setComplextValue with PreserveText doesn't work for me. I have to use cloneBlock to generate lines. Here is my solution:

  1. Update your template with a block definition:
${lines}
${line}
${/lines}
  1. Prepare an array of strings for replacement:
$replacements = [
    ['line' => 'line 1 text'],
    ['line' => 'line 2 text'],
    ...
];
  1. Invoke cloneBlock:
$templateProcessor->cloneBlock('lines', 0, true, false, $replacements);

@mredl
Copy link

mredl commented Feb 1, 2023

I'm not sure why the solution using setComplextValue with PreserveText doesn't work for me. I have to use cloneBlock to generate lines. Here is my solution:

  1. Update your template with a block definition:
${lines}
${line}
${/lines}
  1. Prepare an array of strings for replacement:
$replacements = [
    ['line' => 'line 1 text'],
    ['line' => 'line 2 text'],
    ...
];
  1. Invoke cloneBlock:
$templateProcessor->cloneBlock('lines', 0, true, false, $replacements);

+1 for me.

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

No branches or pull requests