Re: [SMARTY] {section} with array that doesnt start with index 0
| From: | Mark Rogers | Date: | Mon, 19 Aug 2002 10:13:50 +0000 |
| Subject: | Re: [SMARTY] {section} with array that doesnt start with index 0 | ||
| References: | 1 2 3 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-5753@lists.php.net to get a copy of this message | ||
> > However, this problem made me wondering about how to construct an array
> > to use with Smarty. Is it a good idea to take primary keys as indices?
>
> absolutely no. it's just-so as long as you can keep you hands on
> both the code and the templates, but as soon as these two are truly
> separated, you either must document the {section} breakage with
> associative (or non-zero-based) arrays for the template authors, or
> use array_values() everywhere.
Or only use foreach. Personally I have always found the foreach syntax a lot
easier to understand and explain to others, and have pretty much
standardised on it (I can't think of any {section}s in my current project).
That said I do have pretty much total control over both templates and code
and haven't been using Smarty long enough to have the voice of experience,
so my two-penneth is probably worth less than that.
The main reason why I prefer indexed arrays (ie not guaranteed 0-based) is
because that's generally how the data arrives at a code level and I don't
like the overhead of reassigning all arrays to suit the templates. I
absolutely agree that the template writer should not have to worry about
where the data comes from, but if they're using foreach they don't need to
so it doesn't become an issue. {section} breaks if the data doesn't have a
zero-based sequential index, {foreach} is less fussy and therefore less
likely to fail due to a coding error elsewhere - not really a problem as I
only write 100% bug free code but useful in case I ever let anyone else near
it :-)
Mark Rogers, Quarella Ltd