Search code examples
phpdocxopentbs

OpenTBS iteration over an array


I am trying to loop through a simple array and display its information in my docx template with OpenTBS, but can't get it working :(

So I have a farely simple array, that I assign to a block as followed:

$TBS = new \clsTinyButStrong();
$TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);
$TBS->LoadTemplate($template, OPENTBS_ALREADY_UTF8);
$myArray = array(   array( 'name' => 'bedroom'),
                    array( 'name' => 'kitchen'),
                    array( 'name' => 'lounge'),
                );
$TBS->MergeBlock( 'myBlock', $myArray);
$output_file_name = 'C:/testTBS.docx';
$TBS->Show(OPENTBS_FILE, $output_file_name);

In my docx template, I have tried few simple things like this:

[myBlock.name]  => doesnt display anything

Ideally, I want to repeat a whole block, as you can imagine my array will be a bit more complexe.

I have tried that other solution:

[myBlock; block=begin]
    Name : [onshow; myBlock.0.name;]
    Name : [onshow; myBlock.$;]
    Name :  [onshow; myBlock.#;]
    Name :  [onshow; name;]
    Name :  [onshow; BlockName.0;]
[myBlock; block=end]

So that kind of works, as it properly iterates over the array (it displays everything 3 times, which is good considering I have 3 sub arrays). But with that option, I can not find how to display the name, as everything I tried just don't display it, as you can see on my output:

Name : [onshow; myBlock.0.name;]
Name : 
Name :  [onshow; myBlock.#;]
Name :[onshow; name;]
Name :[onshow; BlockName.0;]

Name : [onshow; myBlock.0.name;]
Name : 
Name :  [onshow; myBlock.#;]
Name :[onshow; name;]
Name :[onshow; BlockName.0;]

Name : [onshow; myBlock.0.name;]
Name : 
Name :  [onshow; myBlock.#;]
Name :[onshow; name;]
Name :[onshow; BlockName.0;]

(I have also tried the same thing without the onshow, with no luck)

I am sorry to ask such a simple question as how to iterate over an array, but even with reading over few other posts on the subject, the docs, or going through the exemple they provide, I was not able to find the solution for this simple problem :(

Thanks a lot for your help, let me know if you need more info !


Solution

  • Your snippet at the PHP side looks correct.

    Your standalone piece of template [myBlock.name] should be merged properly with the the first record of your data. That is the TBS behavior when no block bound is defined.

    If nothing is merged, it may be because some design does split some TBS tag. The solution is to select the TBS tag, cut it, then past it as plain text (without formatting).

    There is also a Ms Word Macro for cleaning TBS tags in DOCX but even it is practical, it is in beta version for now.

    Your piece of template with the block definition is not correct. It should be like this :

    [myBlock; block=begin]
        Name : [myBlock.name]
        Name : [myBlock.$]
        Name : [myBlock.#]
        Name : [myBlock.name]
    [myBlock; block=end]