TinyButStrong - the PHP Template Engine
Categories > TinyButStrong general >

Template merging multiple records from array

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs

By: JHula
Date: 2013-06-04
Time: 16:47

Template merging multiple records from array

I have a template with this structure in openoffice:

[opportunity;block=begin][opportunity.opportunity_name_0][opportunity;block=end]
[opportunity;block=begin][opportunity.opportunity_name_1][opportunity;block=end]
[opportunity;block=begin][opportunity.opportunity_name_2][opportunity;block=end]

And I've also tried:

[opportunity;block=text:p][opportunity.opportunity_name_0][opportunity;block=text:p]
[opportunity;block=text:p][opportunity.opportunity_name_1][opportunity;block=text:p]
[opportunity;block=text:p][opportunity.opportunity_name_2][opportunity;block=text:p]

And an array data source that looks like this:
Array
(
    [0] => Array
        (
            [opportunity_id_0] => 4508
            [opportunity_name_0] => order testing opportunity
        )

    [1] => Array
        (
            [opportunity_id_0] => 4e02
            [opportunity_name_0] => aabb
        )

    [2] => Array
        (
            [opportunity_id_0] => 6626
            [opportunity_name_0] => Second testing opportunity
        )

and I've also tried this array structure:

Array
(
    [0] => Array
        (
            [opportunity_id_0] => 4508
            [opportunity_name_0] => order testing opportunity
        )

    [1] => Array
        (
            [opportunity_id_1] => 4e02
            [opportunity_name_1] => aabb
        )

    [2] => Array
        (
            [opportunity_id_2] => 6626
            [opportunity_name_2] => Second testing opportunity
        )

Even if I put it all in one array, it'll only merge the first record.
Array (
    [0] => Array
        (
            [opportunity_id_0] => 4508
            [opportunity_name_0] => order testing opportunity
            [opportunity_id_1] => 4e02
            [opportunity_name_1] => aabb
            [opportunity_id_2] => 6626
            [opportunity_name_2] => Second testing opportunity
        ))

and my php code looks like this:
                $this->TBS->MergeBlock('opportunity', $ops);
where $ops is the name of the above array from my sql query result.

But the only row that will ever merge is the first result, record number 0, and the other rows (where it should show record 1 and 2) are blank. If NoErr is set to false, I get this error:
when merging block "opportunity": unsupported variable type : 'NULL'.

By the way, I know it's not inserting extra formatting in the xml of my openoffice template, I've checked and double-checked.
And I should note, when I use this library to merge a different template using a different array (albeit one of the exact same structure) it will merge multiple records beautifully. I can't figure out why it won't merge multiple records for this template when it works in my other template. Is it possible that my declaration of the headergrp in the other template is what's causing it to merge multiple records there and not in this template? I'd thought use of the headergrp was only for repeating that field every time there's a new record to merge, not that it controlled the merging of new records completely.
Or maybe it only works in the other template because the other template has blocks in a table, and the template that isn't working does not contain a table. Does tbs require a table tag to merge multiple records?
By: JHula
Date: 2013-06-04
Time: 19:59

Re: Template merging multiple records from array

I've also tried it with the exact same table format and syntax as the template that works:


[opportunity.opportunity_id_0;block=table:table-header-rows;parentgrp=opportunity_id_0]
[opportunity.opportunity_name_0;block=table:table-row]
[opportunity.opportunity_name_1]

but it's still not merging anything except the first record.
By: JHula
Date: 2013-06-04
Time: 22:58

Re: Template merging multiple records from array

It's like this line of the manual is determining this behavior, but as far as I know I've defined the block 'opportunity'. Is there something else I need to do in order to define the block, other than putting [opportunity.opportunity_id_0] somewhere in my template?
I don't have the block set to a specific xml tag, is this necessary? When I tried defining the block this way, [opportunity.opportunity_id_0;block=table:table-row] it didn't change the behavior, so is there something I'm doing wrong with the block definition?

Line from manual:
"If no block's definition is found in the template, then the MergeBlock() method will merge the first record with all linked fields found in the template."
By: JHula
Date: 2013-06-05
Time: 18:50

Re: Template merging multiple records from array

Please help if you know anything about this or if you've had a similar problem. I've tried every combination of changes I can think of, I need a fresh perspective on this if you've encountered similar behavior.
By: JHula
Date: 2013-06-05
Time: 20:16

Re: Template merging multiple records from array

This is really strange - when I do a sql query and put it in a table format, it will merge multiple records, but when I use any other tags, like text:p, and it's not from a sql query but is just a normal array, it won't merge multiple records - anyone having this issue with openoffice merging of multiple records without using tables? I want to be able to merge multiple records such as addresses or contacts without using a table to do so, that's why I don't want to use tables. I don't understand why the sql query result array was merging but the normal php array was not, despite being structured the exact same way. It must be a problem with the tags, then - only the tag table:table-row is working for multiple records, whereas text:p is not. I'm going to try inserting custom xml tags to help the library differentiate between blocks - I'll report back here when I know if this will work.
By: Skrol29
Date: 2013-06-05
Time: 23:10

Re: Template merging multiple records from array

Hi,

All the records must have the same structure. That's why the second array (with keys "opportunity_id_0", "opportunity_id_1", "opportunity_id_2") cannot work.
So the first array can be ok.

In the template, yu must have the same colmuns (keys) that the records.
So [opportunity.opportunity_name_1] cannot work.

Make all the records merged, then try with "parentgrp".

But I cannot figure out if you want to merge on table rows or on a piece of text.
By: JHula
Date: 2013-06-05
Time: 23:36

Re: Template merging multiple records from array

I want to merge on text. This is the situation I might want to use, I might have multiple addresses on file, and in my open office template I want to be able to call all of the addresses like this:

"Hello Jamie,

I have multiple addresses on file for you, [contact.address_street_0] [contact.address_city_0] [contact.address_state_0] and [contact.address_street_0] [contact.address_city_0] [contact.address_state_0].
Can you reply to this email with the address you want me to use for shipping?

Thanks."

And my array can be structured any number of ways, like above, or like this:
Array
(
    [0] => Array
        (
            [address_street_0] => x
            [address_city_0] => y
            [address_state_0] => z
        )

    [1] => Array
        (
            [address_street_0] => a
            [address_city_0] => b
            [address_state_0] => c
        )

So I was wondering if I can access both records, not in a table, but in normal text, and if so, how could I declare my block differently, or should I try to use custom tags in openoffice xml? It doesn't seem like it's possible to make custom tags, and using the tag text:p in my block declaration so far hasn't indicated that it can merge multiple records, only one at a time. Is there something other tactic I can try?
This is the custom tag called "sugar" I tried for example but it corrupted the document so I'm not sure you can use custom tags in open office.

<text:p text:style-name="P6">
<sugar>
  [opportunity.name;block=sugar] [opportunity.description]
  <text:span text:style-name="T2">[opportunity.id]</text:span>
  <text:span text:style-name="T2">[opportunity.sales_stage][opportunity.opportunity_type] [opportunity.lead_source] [opportunity.amount] [opportunity.next_step]</text:span>
  </sugar>
  </text:p>
By: Skrol29
Date: 2013-06-05
Time: 23:45

Re: Template merging multiple records from array

The snippet with "block=sugar" is correct for TBS but I guess OpenOffice is considering the <sugar> element as an alien.

Then the syntax with "block=begin"/"block=end" should work but yu have to ensure that those TBS fields will not split XML entities.
For example, they must no be ebbeded in a <text:span>.

This is correct:
<text:p text:style-name="P6">
  [opportunity;block=begin]
  [opportunity.name] [opportunity.description]
  <text:span text:style-name="T2">[opportunity.id]</text:span>
  <text:span text:style-name="T2">[opportunity.sales_stage][opportunity.opportunity_type] [opportunity.lead_source] [opportunity.amount] [opportunity.next_step]</text:span>
  [opportunity;block=end]
</text:p>

This is wrong:
<text:p text:style-name="P6">
  <text:span text:style-name="T2">[opportunity;block=begin]</text:span>
  [opportunity.name] [opportunity.description]
  <text:span text:style-name="T2">[opportunity.id]</text:span>
  <text:span text:style-name="T2">[opportunity.sales_stage][opportunity.opportunity_type] [opportunity.lead_source] [opportunity.amount] [opportunity.next_step]</text:span>
  [opportunity;block=end]
</text:p>
By: JHula
Date: 2013-06-06
Time: 15:49

Re: Template merging multiple records from array

Great, it worked! Thank you so much for making the TBS library, it's wonderful and makes merging documents a simple task. And thank you for the helpful tips in the forums.
Maybe the openoffice community will respond to demand for this and will make the odt document type definition allow custom tags at some point.
Thank you again, everyone like me appreciates all your contributions to the software ecosystem.