Forum


Replies: 1   Views: 411
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