Forum


Replies: 5   Views: 3895
Clone block and replace content
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by admin  · 22-07-2018 - 11:32

Hello,

On https://www.phpdocx.com/documentation/cookbook/clone-blocks you can read how to work with blocks. Also please check the included samples in the package.

On https://www.phpdocx.com/documentation/snippets/clone-blocks-replacing-placeholders is explained how to clone blocks and replace placeholders.

About your code, the problem is that you are cloning and replacing its content. We recommend you to do it using two loops:

foreach ($names as $key => $item) {
  $docx->cloneBlock('NAME');
}
foreach ($names as $key => $item) {
  $docx->replaceVariableByHtml('name', 'block', $item);
}

And use the firstMatch option available in the replace methods.

Anyway, we recommend you to upgrade to phpdocx 8.2 that includes many improvements to work with blocks. If you upgrade to this version, feel free to send to contact[at]phpdocx your template and the dev team will generate a custom code to illustrate how to do it.

Regards.