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

custom iteration block/function

 
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
codezilla
Smarty n00b


Joined: 15 May 2003
Posts: 2

PostPosted: Thu May 15, 2003 8:50 pm    Post subject: custom iteration block/function Reply with quote

Hi. I'm a smarty noob, so I apologize if this is a really basic question.

I need to iterate through an array of objects and display certain properties (retrieved through accessor methods) on each iteration.

The non-smarty version might look something like this:
Code:
<?php
$articles = array(new Article(), new Article(), new Article());

foreach ($articles as $article)
{
?>
    <!-- article -->
    <a href="article.php?id=<?php echo $article->getId(); ?>">
        <?php echo $article->getHeadline(); ?>
    </a>
    <?php echo $article->getDate(); ?><br>
    <!-- article -->
<?php
}
?>


What is the best way to accomplish this type of thing using smarty? Thanks!

-geoff
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu May 15, 2003 10:18 pm    Post subject: Reply with quote

I don't think you need a custom iterator for this particular problem.

Smarty's foreach syntax (to iterate the articles) along with its support for "assigned objects" are just the trick for you. Take a look at the manual under Advanced Topics.
Back to top
View user's profile Send private message
codezilla
Smarty n00b


Joined: 15 May 2003
Posts: 2

PostPosted: Mon May 19, 2003 5:58 pm    Post subject: Reply with quote

Thanks for the reply. You're right, it's a simple case of RTFM. Smile

On a related note, is it possible to create a custom iteration function/block? For example, let's say I wanted to simply change the interface of foreach, but still do the same thing:

Code:

{iterate name="$names" id="name"}
    Name: {$name}
{/iterate}
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon May 19, 2003 6:14 pm    Post subject: Reply with quote

Sort of. Messju recently extended the block function API in the CVS version of the codebase. This change permits the type of use you suggest.

See:

http://www.phpinsider.com/smarty-forum/viewtopic.php?t=228

and

http://www.phpinsider.com/smarty-forum/viewtopic.php?t=291
Back to top
View user's profile Send private message
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