Forum


Replies: 5   Views: 1350
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 DC_Adams  · 20-07-2018 - 14:46

Hi there,

I am hoping that you could please assist me.

I am trying to clone a block and populate it with new data each time it is cloned.

Here is my code:
// code
$docx = new \Phpdocx\Create\CreateDocxFromTemplate('template.docx');

$names = [
  '<strong>Louis Coetzee</strong>',
  '<strong>Dillon Adams</strong>',
];

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

$docx->createDocx('clonedWord.docx');
// code

My template file:
// template file
$BLOCK_NAME$
$name$
$BLOCK_NAME$
// template file

The example shows that I set a block in the template file as specified in the documentation(I hope) and then I try to clone the block in a loop.

I want it to populate the name the way it is now with "Louis Coetzee" and then clone the block "NAME" and populate it with "Dillon Adams" 
so that in the word document it shows only the two names "Louis Coetzee" and "Dillon Adams"

At the moment it only displays the block name variables and Louis Coetzee inside that as described below:
// output
$BLOCK_NAME$
Louis Coetzee
$BLOCK_NAME$
// output

I think the issue is with the way that the block is being defined in the template file, I am just not 100% sure on how to set it. Any advice on getting it to work would be much appreciated.

I am using version 7.0 of phpdocx