RE: [SMARTY] {section} with array that doesnt start with index 0

From: Date: Mon, 19 Aug 2002 10:29:09 +0000
Subject: RE: [SMARTY] {section} with array that doesnt start with index 0
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-5755@lists.php.net to get a copy of this message
Indeed, I was thinking the same about that. For example my news array has the following structure: array $newsListData: -> ['news_id'] (x) -> ['news_topic'] -> ['news_timestamp'] -> ['news_text'] -> ['news_comments'] -> ['news_author'] -> ['comments'] (x) -> ['comment_id'] (x) -> ['comment_timestamp'] -> ['comment_text'] -> ['comment_author'] -> ['comment_author_isMember'] (1/0) I think such a structure is easier to understand (for template designers, ..) than something like this: array $newsListData: -> ['news_id'] -> ['news_topic'] -> ['news_timestamp'] -> ['news_text'] -> ['news_comments'] -> ['news_author'] -> ['comments'] (x) -> ['comment_id'] -> ['comment_timestamp'] -> ['comment_text'] -> ['comment_author'] -> ['comment_author_isMember'] (1/0) In the first construction everything is "grouped" by one newspost / commentpost by the news_id / comment_id. In the second construction, you don't have that. Actually you do, but that depends on how you make your array: $newsListData[0]['news_id'] (here it is also grouped) or $newsListData['news_id'][0] (here it isn't) for the first newspost (see other mail). Peter -----Original Message----- From: Mark Rogers [mailto:mark@quarella.co.uk] Sent: maandag 19 augustus 2002 12:14 To: 'Smarty General' Subject: Re: [SMARTY] {section} with array that doesnt start with index 0 > > 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 -- Smarty General Mailing List (http://smarty.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.smarty.general (#5755) next »