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

template replaceBlock() inserts text wrong in XML #316

Open
soren-bang opened this issue Jul 16, 2014 · 10 comments
Open

template replaceBlock() inserts text wrong in XML #316

soren-bang opened this issue Jul 16, 2014 · 10 comments

Comments

@soren-bang
Copy link

soren-bang commented Jul 16, 2014

Greetings,

I have just recently started using PHPWord and up until now everything works as expected.

Using a simple document to test the template replaceBlock() function gives me a problem - it seems the text is inserted into the XML wrongly.

Template looks like this:

--- TP ---
${TP}

TEST

${/TP}
-- /TP ---

And after executing $document->replaceBlock('TP', 'THIS IS A SMALL TEST'); the result looks like this:

--- TP ---
-- /TP ---

When I examine the XML, the replacement has taken place - it's 'just' placed outside the structure:

<w:p w:rsidR="004F5777" w:rsidRDefault="005F5003">
<w:r>
<w:t>--- TP ---< /w:t>
</w:r>
</w:p>THIS IS A SMALL TEST< w:p w:rsidR="008A240D" w:rsidRDefault="005F5003">
<w:r>
<w:t>--- /TP ---< /w:t>
</w:r>
</w:p>

Am I being a knob and is doing something wrong or is it the code?

Thanks for listening!

:S:


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@Salastn76
Copy link

Hi,
I have the same problem.
Thanks

@alpha1125
Copy link

I'm also have the same problem.

I've tried to creating the template from:

  • Mac Word 2011
  • LibreOffice
  • Google Docs.

All provided templates cause the issue when opening in Mac Word:

"The Open XML file cannot be opened because there are problems with the contents or the file name might contain invalid characters (for example, /).

Details
No error detail available
Location: 2

@samdark
Copy link

samdark commented Jul 31, 2017

webadev@d825ff7 fixes the issue. @troosan is there a chance to get it merged into master in nearby future?

@robov
Copy link

robov commented Sep 12, 2017

Thanks @samdark , looking forward to the merge @troosan so I can actually use the replaceblock

@FBnil
Copy link

FBnil commented Oct 5, 2017

@samdark @robov
I do not find the fix being a fix per-se (it is beautiful code though). You see, when you grab a block, it is actually the whole paragraph you grab, thus <w:p>...${variable}...</w:p>, and thus, the blockReplace() expects you to replace it with an xml block yourself, not "my text".

This is actually the way to do a blockReplace(). Each block in a separate line (so a paragraph)

${myblock}
${variable}
${/myblock}

And so you have a block, and you can cloneBlock("myblock", 1); to get rid of the block tags, and setValue('variable#1', "my text"); (or setValue('variable', "my text"); depending on the version you are on).

This current, intended usage if blockReplace() is to, for example, grab a signature block (xml) from another document and insert it into our document, as setValue() can not do such thing (only text, not formatting; formatting should already be present).

The only case I can think about is having a default value of sorts:

Dear ${gender}sir${/gender}, please find the attachment in the envelope.

with pseudo code:

if($gender == 'man'){
  cloneInlineBlock("gender", 1); // remove the block variables
}else{
   replaceInlineBlock("gender", "Madam"); // Make the text read "Dear Madam, please...."
}

or using it to delete a block without making the text below slide upwards due to a deleted element (similar to replacing all letters with whitespace to keep the formatting exact).

What are your thoughts?

@robov
Copy link

robov commented Oct 6, 2017

The problem I had was the same as the initial poster.... hacking the code with the suggestion of Samdark fixed the issue.
I have inline and non-inline blocks that I want to replace with simple text

@FBnil
Copy link

FBnil commented Oct 8, 2017

@robov can you test my code? TemplateProcessor.php with inline block support. This is a totally different approach (not xpathy but rposy)

@ezequielrds
Copy link

ezequielrds commented Apr 19, 2021

The method cloneBlock acceppts a integer as a second parameter. This parameter set the amount of times the block will be cloned.
So, if you set 0, it will clone "0 times", what means that the block will be replaced by nothing:

$template->cloneBlock("blockname", 0);

It works.

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.

@github-actions github-actions bot added the Stale label Nov 18, 2022
@Progi1984 Progi1984 removed the Stale label Nov 18, 2022
@allestaire
Copy link

The replaceBlock wont show its replacement string to the output doc.

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

No branches or pull requests

9 participants