Page 1 of 1

Template Lite Multidimensional Array

Posted: Tue Nov 21, 2006 10:44 am
by julian_lp
Given the following array, I'm having hard times trying to figuring out how could I do to read its values from the template...

Code: Select all

$T_array = below is the print_r'ed array
Array
(
    [0] => Array
        (
            [link] => ?CALLED_FROM=3&VAL_1=1
            [cat] => Categoria 1
            [arr_subcat] => Array
                (
                    [0] => Array
                        (
                            [link] => ?CALLED_FROM=CALLED_FROM_VER_SUBCATEGORIA&VAL_1=1
                            [subcat] => SUb 1
                        )

                    [1] => Array
                        (
                            [link] => ?CALLED_FROM=CALLED_FROM_VER_SUBCATEGORIA&VAL_1=2
                            [subcat] => Sub categoria 2
                        )

                )

        )

    [1] => Array
        (
            [link] => ?CALLED_FROM=3&VAL_1=3
            [cat] => Interés general
            [arr_subcat] => Array
                (
                )

        )

    [2] => Array
        (
            [link] => ?CALLED_FROM=3&VAL_1=2
            [cat] => Programacion
            [arr_subcat] => Array
                (
                    [0] => Array
                        (
                            [link] => ?CALLED_FROM=CALLED_FROM_VER_SUBCATEGORIA&VAL_1=3
                            [subcat] => Otra Subcategorias
                        )

                )

        )

)

      ///asign the var to the template
	$tpl->assign('T_array', $T_array);
And now, inside the template, I've to loop in the array

Code: Select all

{section name=id loop=$T_array}
	
		link: {$T_array[id].$link} {* It doesn't work *}  <br /> 
		link: {$T_array[id].link} {* It doesn't work *}  <br /> 
		link: {$T_array[id][$link]} {* It doesn't work *}  <br /> 
		link: {$T_array[id][link]} {* It doesn't work *}  <br /> 

{/section}
I think one of the best ways to pass data to the template is through a multidim array, but I'm a little bit dissapointed how different is looping arrays in TL from a PHP point of view :(

Is there any TL gurú in this forum?

Re: Template Lite Multidimensional Array

Posted: Tue Nov 21, 2006 10:58 am
by jayshields
julian_lp wrote:Is there any TL gurú in this forum?
I believe AKA Panama Jack wrote Template Lite, haven't seen him post in a while though.

Staying on topic, I can't help you because I've never used TL. :) Sorry.

Posted: Tue Nov 21, 2006 11:57 am
by AKA Panama Jack
Why are you using the {section} tag? I only added support for it to be compatible with Smarty. If this is your own code you should use something else like the {foreach} tag. The {section} tag is very, very slow and quite frankly is about as stupid a function as could have been created for templating.

Code: Select all

{foreach key=id value=item from=$T_array}
	link: {$T_array[$id].link}  <br /> 
	arr_subcat 0 link: {$T_array[$id].arr_subcat.0.link}  <br /> 
{/foreach}
The above should work just fine for you. I gave an example of also displaying data four elements deep in your array. This is a Smarty variable convention and I copied it with Template Lite. When you are working with multi-dimensional arrays in a template you shouldn't use the PHP brackets [] to separate the elements after the first element. Smarty uses brackets [] for reserved variable access. You should always use the period (.) as a delimiter between each array element.

Do I personally like that?

No.

But that is how Smarty was written and I am trying to stay compatible with Template Lite. Personally I would rather use the PHP syntax for multi-dimensional arrays.

Re: Template Lite Multidimensional Array

Posted: Tue Nov 21, 2006 11:59 am
by AKA Panama Jack
jayshields wrote:
julian_lp wrote:Is there any TL gurú in this forum?
I believe AKA Panama Jack wrote Template Lite, haven't seen him post in a while though.

Staying on topic, I can't help you because I've never used TL. :) Sorry.
I rarely post. :) Usually when I can help on something but not always. ;) I am not one of those people who feel they have to post on every subject on a forum.

Posted: Wed Nov 22, 2006 1:37 am
by julian_lp
AKA Panama Jack wrote:

Code: Select all

{foreach key=id value=item from=$T_array}
	link: {$T_array[$id].link}  <br /> 
	arr_subcat 0 link: {$T_array[$id].arr_subcat.0.link}  <br /> 
{/foreach}
That's what I get doing the above

Code: Select all

link: I
arr_subcat 0 link:
And execution stops as well....
Any idea why?

BTw: foreach seems to be starting from index 1 rather than 0, cause if I write {$item} it prints "interes general" rather than "categoria 1"

Posted: Wed Nov 22, 2006 4:50 am
by julian_lp
Please forget it all.

I just switched to a plain PHP Template, to not to have this kind of problems.
In the near future, I'll take a look again to TL, just to see if I can adapt it to my needs

Thank you all

Posted: Wed Nov 22, 2006 11:01 am
by AKA Panama Jack
It sounds like you have OTHER problems and they are not related to Template Lite.

I took the array you posted above and used the {foreach} code I posted with the lastest version of Template Lite on Sourceforge and it worked like a charm.

It displayed...
link: ?CALLED_FROM=3&VAL_1=1
arr_subcat 0 link: ?CALLED_FROM=CALLED_FROM_VER_SUBCATEGORIA&VAL_1=1
link: ?CALLED_FROM=3&VAL_1=3
arr_subcat 0 link:
link: ?CALLED_FROM=3&VAL_1=2
arr_subcat 0 link: ?CALLED_FROM=CALLED_FROM_VER_SUBCATEGORIA&VAL_1=3
About the only way it wouldn't display the above is if the array you posted is NOT the actual array being sent to the template.

Posted: Wed Nov 22, 2006 11:12 am
by julian_lp
AKA Panama Jack wrote:It sounds like you have OTHER problems and they are not related to Template Lite.

I took the array you posted above and used the {foreach} code I posted with the lastest version of Template Lite on Sourceforge and it worked like a charm.

It displayed...
link: ?CALLED_FROM=3&VAL_1=1
arr_subcat 0 link: ?CALLED_FROM=CALLED_FROM_VER_SUBCATEGORIA&VAL_1=1
link: ?CALLED_FROM=3&VAL_1=3
arr_subcat 0 link:
link: ?CALLED_FROM=3&VAL_1=2
arr_subcat 0 link: ?CALLED_FROM=CALLED_FROM_VER_SUBCATEGORIA&VAL_1=3
About the only way it wouldn't display the above is if the array you posted is NOT the actual array being sent to the template.
Please excuse me, I should've written it, but you're absolutely right. I wasn't passing this array, you know, those kind of silly mistakes that could drive you to insanity :x

The key point why I switched was when I saw

Code: Select all

{$T_array[$id].arr_subcat.0.link}
cause I dont know previously whether there will be 0, 1, 2 or more elements, and I have absoutely no idea if it is possible do a nested foreach in this situation

Once again, excuse me for not telling you that I was wrong with the original array.

Posted: Wed Nov 22, 2006 11:23 am
by AKA Panama Jack
Sure you can do nested {foreach} without a problem.

Code: Select all

{foreach key=id value=item from=$T_array} 
        link: {$T_array[$id].link}  <br /> 
        {foreach key=id2 value=item from=$T_array[$id].arr_subcat} 
                arr_subcat {$id2} link: {$T_array[$id].arr_subcat.$id2.link}  <br /> 
        {/foreach}
 {/foreach}
The thing you have to remember with Smarty syntax is you can use brackets [] around the first element but every element after that you have to use periods. Like I said before damned silly but that is how they designed Smarty and I have to copy that format to stay compatible.