Charles Kirk
Charles Kirk

Reputation: 167

OpenTBS Mail merging .docx using PHP

I'm using http://www.tinybutstrong.com/plugins/opentbs/demo/demo.html and having trouble getting it to work. My .docx has real word mail merge fields. I've been trying to understand the documentation, all I can get out of it, is that the PHP demo code, seems to declare $your_name, and then it magically replaces onshow.your_name in the .docx.

In the first instance, I thought I used MergeBlock with an array of $data. Here's my code so far:

  $TBS = new clsTinyButStrong;
  $TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);

  $template = $_SERVER['DOCUMENT_ROOT'] . '/inc/dd.docx';
  $data = array();
  $data[] = array('ContactName'=>$this->title . ' ' . $this->firstname . ' ' . $this->surname,
                  'Address1'=>$this->address1,
                  'Address2'=>$this->address2,
                  'Address3'=>$this->town,
                  'Address4'=>$this->county,
                  'PostalCode'=>$this->postcode,
                  'Bacsref'=>$this->bb_number,
                  'Account_Name'=>$this->ac_name,
                  'SortCode'=>$this->CorrectedSortCode,
                  'Account_Number'=>$this->CorrectedAccountNumber);
  $ContactName = $this->title . ' ' . $this->firstname . ' ' . $this->surname;
  $TBS->LoadTemplate($template);
  $TBS->MergeBlock('a,b', $data);                
  $file_name = $this->bb_number . ' Direct Debit';
  //$TBS->Plugin(OPENTBS_DEBUG_XML_CURRENT);
  $TBS->Show(OPENTBS_DOWNLOAD, $file_name . '.docx');

The downloaded file hasn't got any of the mail merge fields replaced. From the demo, I can't infer how onshow.your_name, which doesn't look like a real word mail merge field, is replaced? All I see is some error checking code to determine $your_name...

Upvotes: 0

Views: 4684

Answers (1)

Skrol29
Skrol29

Reputation: 5597

OpenTBS is a plug-in for the TinyButStrong template engine (also called TBS) . TBS merges text/html/xml contents, while OpenTBS+TBS merges Docx, Xlsx, Pptx, Odt, Ods, ...

This is why, the templating syntax you need is in fact in the TinyButStrong manual.

For example:

$your_name is merged in the docx with the tag [onload.your_name] because all [onload.*] is an automatic field which will be merged when you call $TBS->Show(), and it will be merged with the corresponding PHP global variables.

If in your PHP you did not defined any global variable named $your_name, then TBS will raise an error because it cannot merge [onload.your_name].

About MergeBlock():

There is the same data/file correspondence. That is: when you code $TBS->MergeBlock('a,b', $data); TBS will search two blocks named 'a' and 'b' and will merged their fields with $data. So you can use [a.ContactName], [a.Address1], ... because of the structure of $data. But the fields of the Docx template given with the package is no more valid because the structure of $data is not the same.

You can have a look of TBS on-line examples, there is many fields and block syntaxes examples.

Upvotes: 3

Related Questions