Forum


Replies: 1   Views: 442
Cloneblocks and loops
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  · 07-09-2023 - 08:11

Hello,

Please note the following when using cloneBlock:

  • The new cloned block is added immediately after the existing cloned block.
  • The occurrence option sets the block position to be cloned in the document.

We also recommend you check the documentation available on Clone blocks replacing placeholders and the samples included in the package, that illustrate the approaches to clone blocks replacing contents and all options.

Instead of using the firstMatch option that requires calculating the exact placeholders to be replaced (removing existing placeholders if needed), we recommend you use the variablesBlock option to do the replacements at the same time the block is cloned. Using firstMatch with cloneBlock, the recommendation is first clone the blocks and then replace the placeholders as detailed in the samples.

The following code generates the needed output using your array:

// new cloned blocks are added after the existing block to be cloned. Reverse it to get the correct order
$reversedParcel = array_reverse($parcel);

foreach ($reversedParcel as $p) {
    $docx->cloneBlock('COT_PARCEL', 1, array($p), array('removeBlockPlaceholder' => true));
    if (!empty($p['COT'])) {
        $docx->cloneBlock('COT_RECORD', 2, $p['COT'], array('removeBlockPlaceholder' => true)); // ignore the first subblock occurrence, that is the block used to create new blocks
    }
}

// delete remaining blocks
$docx->deleteTemplateBlock('COT_RECORD');
$docx->deleteTemplateBlock('COT_PARCEL');

Regards.