I’m trying to export a template.docx file in my PHP project using OpenTBS.
This is the source in my .php file (of course the array would be generated dynamically from a query, this is just a dummy):
$surname_list = array(
'smith' => array(
array(
'n_name' => 'jhon',
'n_surname' => 'smith',
),
array(
'n_name' => 'mark',
'n_surname' => 'smith',
),
),
'cole' => array(
array(
'n_name' => 'jenny',
'n_surname' => 'cole',
),
array(
'n_name' => 'dennis',
'n_surname' => 'cole',
),
array(
'n_name' => 'paul',
'n_surname' => 'cole',
),
),
);
$TBS->MergeBlock('table_block', $surname_list);
And that’s what I have in my template.docx:
[table_block;block=begin]
[block.tbs:table]
Name | Surname
[n_name] | [n_surname]
[block.tbs:table;block=end]
[table_block;block=end]
Is even possible to achieve something like this?
Name | Surname |
---|---|
jhon | smith |
mark | smith |
Name | Surname |
---|---|
jenny | cole |
dennis | cole |
paul | cole |
In the documentation I didn’t find something useful for my purpose.
This is the only thing I archived in my template.docx, it does indeed iterate the "main" array, but nothing more:
[block.tbs:table]
Name | Surname
[n_name] | [n_surname]
[block.tbs:table;block=end]
[block.tbs:table]
Name | Surname
[n_name] | [n_surname]
[block.tbs:table;block=end]
UPDATE
by changing the array this way:
$surname_list = array(
array(
'surname' => 'smith',
'people' => array(
array(
'n_name' => 'jhon',
'n_surname' => 'smith',
),
array(
'n_name' => 'mark',
'n_surname' => 'smith',
),
),
),
array(
'surname' => 'cole',
'people' => array(
array(
'n_name' => 'jenny',
'n_surname' => 'cole',
),
array(
'n_name' => 'dennis',
'n_surname' => 'cole',
),
array(
'n_name' => 'paul',
'n_surname' => 'cole',
),
),
),
);
$TBS->MergeBlock('table_block', $surname_list);
and the template.docx:
[table_block;block=begin]
[table_block.surname]
[table_block;block=end]
I managed to print:
smith
cole
Still no success on printing the "people".
2
Answers
I managed to solve the problem by editing the array structure this way:
PHP
template.docx
Result
For further infos: Automatic subblocks
Keep in mind that
block=tr
won't work if you try to export a .docx,block=tbs:row
is to be used!There are several ways of doing repeated tables with TBS.
You can use Sub-blocks or Grouping with parent, depending the structure of your data.
See also the doc for Sub-blocks, and Groupings.
Example 1 : (simplest) with parameter
headergrp
template :
Example 2 : with automatic-sub blocks
template :
Example 3 : (more complicated) sub-block with dynamic query
template :