|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
codezilla Smarty n00b
Joined: 15 May 2003 Posts: 2
|
Posted: Thu May 15, 2003 8:50 pm Post subject: custom iteration block/function |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu May 15, 2003 10:18 pm Post subject: |
|
|
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 |
|
codezilla Smarty n00b
Joined: 15 May 2003 Posts: 2
|
Posted: Mon May 19, 2003 5:58 pm Post subject: |
|
|
Thanks for the reply. You're right, it's a simple case of RTFM.
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
|
Back to top |
|
|
|
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
|