Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Best practice for rendering an array of mixed object types

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
mr_ishbu
Smarty n00b


Joined: 15 Apr 2010
Posts: 1

PostPosted: Thu Apr 15, 2010 3:48 am    Post subject: Best practice for rendering an array of mixed object types Reply with quote

Say I have an array of mixed objects. I want to iterate over these objects and render each object with a different template partial based on that object type.

For example when rendering a news feed:

Code:

// Feed item is base class and there can be different types of stories for the feed
class FeedItem {}
class PhotoFeedItem extends FeedItem {}
class StatusFeedItem extends FeedItem {}

// Feed becomes
$feed = array();
$feed[] = new PhotoFeedItem();
$feed[] = new StatusFeedItem();

$smarty->assign('feed', $feed);


Now when rendering that list of feed items, I want to be able to include a partial based on the class type. An example hacky/bad solution:

Code:

{section name=item loop=$feed}
  {if get_class($feed[item]) == "PhotoFeedItem"}
     {include file="feed/partials/photo.tpl"}
  {/if}
{/section}



Obviously this is not very clean or extensible. There are several different ways I've thought about approaching this, but am looking for suggestions on best practices for this type of problem.
Back to top
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Thu Apr 15, 2010 8:35 pm    Post subject: rewrite Reply with quote

I suggest you to use different array variables, with homogeneous data, and handle/show them accordingly into your template.

If you want to use same variable name, you can use string-indices to so that you can know it in the template as different entity.

For example,
Code:
$feed = array();
$feed['photo'] = new PhotoFeedItem();
$feed['status'] = new StatusFeedItem();

# Use assign by reference, if the size of the data is too big.
$smarty->assignByRef('feed', $feed); # On Smarty 3
$smarty->assign_by_ref('feed', $feed); # On Smarty 2.6


Loop through different pieces of the data, so you have better control while rendering the page. By this way, you can prevent to check for your unique class name to include a file.

Also, within the template, do not use {include} - because, you may not now how many times the loop will run for, which may affect the performance. Instead, copy/paste the contents of feed/partials/photo.tpl file within the block.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP