Categories > TinyButStrong general >

trouble with MergeBlock method and an array

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: John
Date: 2011-02-18
Time: 20:55

trouble with MergeBlock method and an array

I've used MergeBlock in the following manner to create TBS variable named 'product'
$TBS->MergeBlock('product', $product->build());

When I create a template with the code
[product.key;block=p]
all the array key names I expect are listed.
One of these array keys has the name description.
When I change this template code to say
[product.description;block=p]
I get a TBS error that says description is not an existing key in the array.
Why does this occur when
[product.key]
outputs description as a key name?
By: Skrol29
Date: 2011-02-19
Time: 01:04

Re: trouble with MergeBlock method and an array

Hi John,

What is the array (or object) structure returned by $product->build() ?
By: John
Date: 2011-02-19
Time: 03:15

Re: trouble with MergeBlock method and an array

$product->build() returns an array

Here's the structure of the product object according to my debugger:
product                               Product(object)
mProduct                            (array)
product_id     97                   (string)
name          Birds                 (string)
description  Is your heart . . .  (string)
etc

Now when I put product.key in my template I get product_id, name, description etc output.
When I put product.name or product.description I get errors.
By: Skrol29
Date: 2011-02-19
Time: 14:09

Re: trouble with MergeBlock method and an array

Hi,

If your data was an array of objects and "description" is a property of such objects, then [product.description] should be displayed with no problem.
In order to understand what is wrong with the [product.description] field, we have to know how the data is structured precisely.
TBS says "description is not an existing key in the array" which means TBS sees the provided data as a recordset of arrays, probably an array of arrays. Lets say an "array record source" of "array records".
However, the column "key" may be an existing column of your array records, but it also may be keyword recognized by TBS for records that are not arrays. Theye are two keywords for such records: key and val.

Can you do a:
var_export($product->build()); exit;
in order to have a complete visibility of the data ?
By: John
Date: 2011-02-19
Time: 14:24

Re: trouble with MergeBlock method and an array

var_export yields the following:

array ( 'product_id' => '65', 'name' => 'Afghan Flower', 'description' => 'This beautiful image was issued to celebrate National Teachers Day. Perhaps you know a teacher who would love this T-shirt?', 'price' => '18.50', 'discounted_price' => '16.99', 'image' => 'afghan-flower.gif', 'image_2' => 'afghan-flower-2.gif', )
By: John
Date: 2011-02-19
Time: 21:22

Re: trouble with MergeBlock method and an array

I see that using MergeField instead of MergeBlock lets me use a template file as I expected. I guess MergeField lets me access an object's properties. Is there a performance issue
with using MergeField instead of MergeBlock or any reason I should make MergeBlock work?
By: Skrol29
Date: 2011-02-19
Time: 21:52

Re: trouble with MergeBlock method and an array

Hi,

There is no performance issue between MergeField() and MergeBlock() for a single record. The merging process is the same, the difference is that they don't merge different structures of data.
Since your data is a standard array with simple items (key/values) then MergeField is better if you know the keys to merge. MergeBlock is better to have a complete listing of keys and values.

You can also save your raray in a global PHP variable, and then you will no longer need to use MergeField. The automatic fields onload and onshow will be ok.