Forum


Replies: 3   Views: 527
How to add variables in an external file
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 ElodieMllr  · 18-04-2023 - 09:51

I have created a new docx from template with mulitple variables.

Then, in the new docx, i need to add an external templates (this is working fine)

    $docx->replaceVariableByExternalFile(array('EXTERNALFILE' => public_path() . '/templates/external file.docx'), array('matchSource' => true));

But now, I need to replace variable by text in the external file.

I have already tried to : 

1. create an array for the external files variables :

  $variableForExternaFile = array(

            'TIMEESTIMATED' => $request->get('DescriptionSheet')['timeEstimated'],

            'HOURLYRATE' => $request->get('PriceInfo')['hourPrice'],

            'TOTALCOST' => $request->get('PriceInfo')['totalCoast'],

            'DISCOUNT' => $request->get('PriceInfo')['reductionAmount'],

            'HOURLYRATEDISCOUNT' => $request->get('PriceInfo')['hourlyRateDiscounted']

        );

2. create a second docx from template for the external file

   $externalFile = new CreateDocxFromTemplate(public_path() . '/templates/external file.docx');

3. replace the variable by text  in the external files 

  $externalFile ->replaceVariableByText($variableForExternaFile );

4. finally replaceVariableByExternalFile in the new docx (external files) 

 $docx->replaceVariableByExternalFile(array('EXTERNALFILE' => $externalFile ), array('matchSource' => true));

But not working.

 

Do I have a solution with phpdocx? 

Posted by ElodieMllr  · 18-04-2023 - 09:59

Deleted by ElodieMllr · 18-04-2023 - 10:00

Posted by ElodieMllr  · 18-04-2023 - 09:59

Resolved :

 

I have first get the external template, then insert the variables and create a copy with the variables.

Finally, insert the new template with the variables in my docx

 

  $externalDocx = new CreateDocxFromTemplate(public_path() . '/templates/external file.docx');


 

        $externalVariables = array(

            'TIMEESTIMATED' => $request->get('DescriptionSheet')['timeEstimated'],

            'HOURLYRATE' => $request->get('PriceInfo')['hourPrice'],

            'TOTALCOST' => $request->get('PriceInfo')['totalCoast'],

            'DISCOUNT' => $request->get('PriceInfo')['reductionAmount'],

            'HOURLYRATEDISCOUNT' => $request->get('PriceInfo')['hourlyRateDiscounted']

        );

        $externalDocx->replaceVariableByText($externalVariables );

        $externalDocx->createDocx(public_path() . '/templates/external file edit.docx');

 

        $docx->replaceVariableByExternalFile(array('EXTERNALFILE' => public_path() . '/templates/external file edit.docx'), array('matchSource' => true));

Posted by admin  · 18-04-2023 - 10:11

Hello,

Yes, as you explain, first you need to replace the placeholders in the external file and then add it to the new DOCX. Or you can use merging features to merge DOCX documents and replace placeholders any time.

Regards.