Forum


Replies: 1   Views: 4746
Remove line where empty variable was
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 jamie3000  · 28-09-2016 - 15:26

Hi Guys,

I'm evaluating the software and have a quick question.

I'm sure there is an easy way to do this but I can't figure it out.

Say I have an address like so

$Address1$
$Address2$
$Address3$
$Address4$
$Address5$
$PostCode$

I'm trying to get it to remove the empty lines if address5 or address4 is an empty string. So far my code looks like the below but it doesn't seem to work.

foreach($variables as $VarToCheckRemove => $varValue)
{
      if(strlen($varValue) == 0) 
       {
            $docx->removeTemplateVariable($VarToCheckRemove, 'block');
       }
 }
        
$docx->clearBlocks();
        
$docx->replaceVariableByText($variables);

$docx->createDocxAndDownload(resource_path('letters/letter'));

 

Posted by admin  · 29-09-2016 - 06:56

Hello,

We recommend you to check the included example in the folder Templates/removeTemplateVariable.

When you write the placeholder you don't write the symbols that wrap the placeholders. You need to remove placeholder using this line:

$docx->removeTemplateVariable('OTHERVAR', 'block');

and not:

$docx->removeTemplateVariable('$OTHERVAR$', 'block');

Also please check that your script is running correctly and doesn't have any error; maybe this is not correct and the removeTemplateVariable method is not being called:

if(strlen($varValue) == 0) 

Regards.