Replies: 2   Views: 148
I want to remove block labels only but $docx->clearblocks() removes label and content

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']);
    $arrayValues = array();
    foreach ($Substitutions as $documentVariable) {
        $arrayValues[$documentVariable] = $row[$documentVariable];

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

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


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.


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