Forum


Replies: 2   Views: 999
I want to remove block labels only but $docx->clearblocks() removes label and 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 tmm  · 14-06-2020 - 16:22

In my template I have blocks that have alternative content. I want to dynamically delete some blocks and leave the otthers.

In the code below, I am removing block content that is not applicable to the type of document. When I omit the $docx->clearBlocks(); statement, the remaining block labels and content show in the generated document. When I include the statement, the remaining block labels and content are removed.

What am I doing wrong?

    $docx = new CreateDocxFromTemplate($FH['TEMPFILEPATH']);
    $docx->setTemplateSymbol('|');
 
    $arrayValues = array();
    foreach ($Substitutions as $documentVariable) {
        $arrayValues[$documentVariable] = $row[$documentVariable];
    }
     $docx->replaceVariableByText($arrayValues);

    foreach ($BlockRules as $k=>$v) {
        if (! eval("return($v);")) {
            $docx->deleteTemplateBlock($k);
            #echo "$k:delete\n";
        }
    }
    
    $docx->clearBlocks();
 

Posted by admin  · 14-06-2020 - 16:42

Hello,

clearBlock removes the whole paragraphs where block placeholders exist, so if your contents are in the same paragraph they will be removed.

We recommend you to use removeTemplateVariable doing an inline type clean, for example:

$docx->removeTemplateVariable('BLOCK_A', 'inline');

If you need to get the existing placeholders please use getTemplateVariables.

Regards.

Posted by tmm  · 14-06-2020 - 17:48

Thank-you