Forum


Replies: 1   Views: 408
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 gtassinari  · 07-09-2023 - 02:18

Hello I am using PHPDOCX advanced version 14. Running into issue with loops and clone blocks and replacing texts. 

Here is my Template

${BLOCK_COT_PARCEL}
${PARCEL}

        
        ${BLOCK_COT_RECORD}

    DEED TYPE:                      ${DEED}
        GRANTEE:                            ${GRANTEE}
        GRANTOR:                            ${GRANTOR}
        DATE EXECUTED:                  ${DATE_EXECUTED}
        DATE RECORDED:                  ${DATE_RECORDED}
        INSTRUMENT NO:                  ${INSTRUMENT_NO}


${BLOCK_COT_RECORD}

        
${BLOCK_COT_PARCEL}

Here is my code

    {
        $docx = new CreateDocxFromTemplate(public_path().'/template.docx', array('parseMode' => true));
        $docx->setTemplateSymbol('${', '}');
        $faker = Factory::create();
        $parcel = array(
            array(
                'PARCEL' => '1111-1111',
                'COT' => array(
                    array(
                        'DEED' => 'ab',
                        'GRANTOR' => 'cd',
                        'GRANTEE' => 'ef',
                        'DATE_EXECUTED' => '1985-10-20',
                        'DATE_RECORDED' => '1999-06-16',
                        'INSTRUMENT_NO' => '12345'
                    ),
                    array(
                        'DEED' => 'hh',
                        'GRANTOR' => 'xy',
                        'GRANTEE' => 'uv',
                        'DATE_EXECUTED' => '1972-09-02',
                        'DATE_RECORDED' => '1972-09-02',
                        'INSTRUMENT_NO' => '34567'
                    ),
                )
            ),
            array(
                'PARCEL' => '1111-2222',
                'COT' => array(
                    array(
                        'DEED' => 'yy',
                        'GRANTOR' => 'xy',
                        'GRANTEE' => 'uv',
                        'DATE_EXECUTED' => '1972-09-02',
                        'DATE_RECORDED' => '1972-09-02',
                        'INSTRUMENT_NO' => '34567'
                    ),
                )
            ),
            array(
                'PARCEL' => '1111-3333',
            ),

        );
        $options = array('parseLineBreaks' => true, 'firstMatch' => true);

        foreach ($parcel as $p) {
            $docx->cloneBlock('COT_PARCEL', 1, array(), array('removeBlockPlaceholder' => true));
            $docx->replaceVariableByText($p, $options);
            if (!empty($p['COT'])) {
                foreach ($p['COT'] as $cot) {
                    $docx->cloneBlock('COT_RECORD', 1, array(), array('removeBlockPlaceholder' => true));
                    $docx->replaceVariableByText($cot, $options);
                }
            }

        }
        $docx->deleteTemplateBlock('COT_PARCEL');
        $docx->deleteTemplateBlock('COT_RECORD');



        $docx->createDocx('example_replaceVariableByText_1');

    }
}

Here is the Output:

1111-1111

        

    DEED TYPE:                      ab
        GRANTEE:                            ef
        GRANTOR:                            cd
        DATE EXECUTED:                  1985-10-20
        DATE RECORDED:                  1999-06-16
        INSTRUMENT NO:                  12345



    DEED TYPE:                     ab
        GRANTEE:                           ef
        GRANTOR:                           cd
        DATE EXECUTED:                  1985-10-20
        DATE RECORDED:                  1999-06-16
        INSTRUMENT NO:                  12345



DEED TYPE:                             hh
        GRANTEE:                           uv
        GRANTOR:                           xy
        DATE EXECUTED:                  1972-09-02
        DATE RECORDED:                  1972-09-02
        INSTRUMENT NO:                  34567



        
1111-1111

        

    DEED TYPE:                      ab
        GRANTEE:                            ef
        GRANTOR:                            cd
        DATE EXECUTED:                  1985-10-20
        DATE RECORDED:                  1999-06-16
        INSTRUMENT NO:                  12345



DEED TYPE:                      hh
        GRANTEE:                        uv
        GRANTOR:                        xy
        DATE EXECUTED:                  1972-09-02
        DATE RECORDED:                  1972-09-02
        INSTRUMENT NO:                  34567



        
1111-2222

        

        

 

Expected Output

1111-1111

        

    DEED TYPE:                      ab
        GRANTEE:                            ef
        GRANTOR:                            cd
        DATE EXECUTED:                  1985-10-20
        DATE RECORDED:                  1999-06-16
        INSTRUMENT NO:                  12345



    DEED TYPE:                      hh
        GRANTEE:                            uv
        GRANTOR:                            xy
        DATE EXECUTED:                  1972-09-02
        DATE RECORDED:                  1972-09-02
        INSTRUMENT NO:                  34567







        
1111-2222

        


    DEED TYPE:                      yy
        GRANTEE:                            uv
        GRANTOR:                            xy
        DATE EXECUTED:                  1972-09-02
        DATE RECORDED:                  1972-09-02
        INSTRUMENT NO:                  34567



        
1111-3333

        

        

Could you please help me out on why the above code doesnt work. I have been racking my brain for a bit now and the loops and cloneblocks doesnt seem to work. If you can provide some guidance that would be great

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.