TinyButStrong - the PHP Template Engine
Categories > TinyButStrong general >

Adapting example to docx, help needed!

By: BB123
Date: 2016-10-18
Time: 11:28

Adapting example to docx, help needed!

Hi all,

I have spent the last few days trying to come to terms with this template. I have looked through and played with the given examples, i'm trying to adapt the emailing given example to basically output a word document.
My php code is as follows..

<?php

include_once('tbs_class.php');
include_once('tbs_plugin_opentbs.php');

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

// data
$data = array();
$data[0] = array('email'                    =>'bb@bb.com',
                 'firstname'                =>'Brad',
                 'lastname'                 =>'bradson',
                 'receiptno'                =>'144543');

$data[0]['products'][] = array('product'    =>'Macbook Pro 13" Retina',
                               'qty'        =>1,
                               'uprice'     =>1200.00);

$data[0]['products'][] = array('product'    =>'PHP For noobs',
                               'qty'        =>1,
                               'uprice'     =>11.0);

$data[0]['products'][] = array('product'    =>'20 Marlboro Gold',
                               'qty'        =>1,
                               'uprice'     =>5.99);

$data[1] = array('email'                    =>'JS@JS.com',
                 'firstname'                =>'Josh',
                 'lastname'                 =>'Smith',
                 'receiptno'                =>'144543');

$data[1]['products'][] = array('product'    =>'Meditation for noobs',
                               'qty'        =>1,
                               'uprice'     =>12.00);

$data[1]['products'][] = array('product'    =>'JQuery for noobs',
                               'qty'        =>1 ,
                               'uprice'     =>15.0);

$data[1]['products'][] = array('product'    =>'Vape',
                               'qty'        =>1,
                               'uprice'     =>69.99);

// Load template
$template = '\email_template.docx';
$TBS->LoadTemplate($template, OPENTBS_ALREADY_UTF8); // Also merge some [onload] automatic fields (depends of the type of document).

// Debug
if (isset($_POST['debug']) && ($_POST['debug']=='current')) $TBS->Plugin(OPENTBS_DEBUG_XML_CURRENT, true); // Display the intented XML of the current sub-file, and exit.
if (isset($_POST['debug']) && ($_POST['debug']=='info'))    $TBS->Plugin(OPENTBS_DEBUG_INFO, true); // Display information about the document, and exit.
if (isset($_POST['debug']) && ($_POST['debug']=='show'))    $TBS->Plugin(OPENTBS_DEBUG_XML_SHOW); // Tells TBS to display information when the document is merged. No exit.

$subject = $TBS->TplVars['subject']; // sets subject variable from template

// merge
foreach ($data as $recipiant)
{
    $TBS->MergeField('i', $recipiant);
    $TBS->MergeBlock('a', $recipiant['products']);
    $TBS->Show(TBS_NOTHING); // merge automatic TBS fields
    $body = $TBS->Source;
    $txt = 'To: '.$recipiant['email']."\r\n".'Subject: '.$subject."\r\n".$body."\r\n\r\n============================================\r\n\r\n";
    $TBS->Source = '<html><head></head><body><div id="main-body"><pre>'.$txt.'</pre></div></body></html>';
    $TBS->Show(OPENTBS_DOWNLOAD); // Also merges all [onshow] automatic fields.
    // Be sure that no more output is done, otherwise the download file is corrupted with extra data.
}

?>

And my docx template is as follows:
[onload;tplvars;subject=Hi!;comm=_]

Dear [i.firstname] [i.lastname],
this is your order receipt.

Receipt #[i.receiptno].

~~~~~~~~~~~~~~~ [a.#;block=begin;comm=_] ~~~~~~~~~~~~~~~
Product #[a.#]
----------
Product:    [a.product]
Quantity:   [a.qty]
Price:      £[a.uprice;frm=0.00]

~~~~~~~~~~~~~~~ [a.#;block=end;comm=_]   ~~~~~~~~~~~~~~~

Thankyou & Have a nice day [i.firstname]!

www.nonexistentshop.org



As of now, a blank word document is output. Any help would be much appreciated!

Thanks,



By: Skrol29
Date: 2016-10-18
Time: 14:23

Re: Adapting example to docx, help needed!

Hi BB123,

I can see many points.

A docx contains many XML sub-file. Mixing an XML body from a DOCX within HTML cannot produce any valid content. This is a kind of heretic.

Your template contains some parameters "comm=_" but this parameter means the TBS fields is bounded on the text line. I mean the binary text line, delimited with characters NewLine and CarriageReturn, not the Ms Word line. Since the inner XML often contains only one text line, it means that parameter "comm=_" probably deleted the whole content of the XML.
You'd better use "enlarge=tbs:p" nested in an Ms Word paragraph, instead of  "comm=_".

An HTML download can be processed only once for a PHP script. This is because 1 download = 1 HTTP query.
So multiple command $TBS->Show(OPENTBS_DOWNLOAD); simply cannot work. This will produce an invalid HTTP reply.

Can you explain what result you're trying to achieve ?



By: BB123
Date: 2016-10-18
Time: 16:45

Re: Adapting example to docx, help needed!

Hi Skrol29,

Thanks for the feedback.

With the above data i initially posted, the result I am trying to achieve would be a docx download that is as follows;

To: bb@bb.com
Subject: Hi!

Dear Brad Bradson,
this is your order receipt.

Receipt #144543.

Product #1
----------
Product:    Macbook Pro 13" Retina
Quantity:   1
Price:      £1200.00

Product #2
----------
Product:    PHP For noobs
Quantity:   1
Price:      £11.00

Product #3
----------
Product:    20 Marlboro Gold
Quantity:   1
Price:      £5.99


Thankyou & Have a nice day Brad!

www.nonexistentshop.org

Then once I have got that to work I will try to get it to do one for each data found.
I'll keep playing with it!
By: Skrol29
Date: 2016-10-22
Time: 00:47

Re: Adapting example to docx, help needed!

Do you want several DOCX, one per recipient, or do you want only one DOCX with one page per recipient ?