Categories > OpenTBS with DOCX >

How to define non global variable in header and footer

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: jhuon
Date: 2011-04-27
Time: 15:57

How to define non global variable in header and footer

I recently discovered OpenTBS and after several tries I can not define variables in header or footer.
How to do without using global variables? In what order should I set the variables and call the correct template?
Here's my code:
$TBS = new clsTinyButStrong();
$TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);

$TBS->LoadTemplate('/Users/jhuon/Public/Drop Box/demo_ms_word.docx#word/document.xml;word/footer1.xml');

$val['yourname'] = 'John';
$data = array();
$data[] = array('firstname'=>'Sandra', 'name'=>'Hill', 'number'=>'1523d', 'picture' => 'ipad2' );
$data[] = array('firstname'=>'Roger', 'name'=>'Smith', 'number'=>'1234f', 'picture' => 'macbookpro' );
$data[] = array('firstname'=>'William', 'name'=>'Mac Dowell', 'number'=>'5491y', 'picture' => 'ipad2_smartcover' );

$TBS->MergeBlock('a,b', $data);
$TBS->MergeField('c', $val);
$TBS->MergeField('yourname', $val['yourname']);

//$TBS->LoadTemplate('#word/footer1.xml');
$TBS->Show(OPENTBS_DOWNLOAD);
By: Skrol29
Date: 2011-04-28
Time: 00:27

Re: How to define non global variable in header and footer

Hi Jhuon,

You're close. You have the load the sub-file, and then do MergeBlock().

Example:
// open the template
$TBS->LoadTemplate('/Users/jhuon/Public/Drop Box/demo_ms_word.docx');

... // usual merging in the body of the document

// now merging the footer
$TBS->LoadTemplate('#word/footer1.xml');

$TBS->MergeBlock('a,b', $data);
$TBS->MergeField('c', $val);
$TBS->MergeField('yourname', $val['yourname']);

... //

// end merging and output the document
$TBS->Show(OPENTBS_DOWNLOAD);

By: jhuon
Date: 2011-04-28
Time: 09:23

Re: How to define non global variable in header and footer

Hi Skrol29,
Thanks for your reply.
I don't know why but it's not working. Here's my code :
$TBS = new clsTinyButStrong();
$TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);

// Main document data
$TBS->LoadTemplate('/Users/jhuon/Public/Drop Box/demo_ms_word.docx');

$data = array();
$data[] = array('firstname'=>'Sandra', 'name'=>'Hill', 'number'=>'1523d', 'picture' => 'ipad2' );
$data[] = array('firstname'=>'Roger', 'name'=>'Smith', 'number'=>'1234f', 'picture' => 'macbookpro' );
$data[] = array('firstname'=>'William', 'name'=>'Mac Dowell', 'number'=>'5491y', 'picture' => 'ipad2_smartcover' );

$someOtherData = array();
$someOtherData['yourname'] = 'John';

$TBS->MergeBlock('a,b', $data);
$TBS->MergeField('c', $someOtherData);

// Footer data
$TBS->LoadTemplate('#word/footer1.xml');

$TBS->MergeField('yourname', 'Bob');

// Download document
$TBS->Show(OPENTBS_DOWNLOAD);
First when I open the generated document, MS Word tells that the document is corrupted (Word found unreadable content).
Once the document is opened, all the $data and $someOtherData fields are filled. But the "yourname" field in the footer remains desperatly [username].
By: Skrol29
Date: 2011-04-28
Time: 21:53

Re: How to define non global variable in header and footer

try with '#word/footer2.xml', it should work.