wh开发者_如何学Goat could be better for respect the MVC?
make a view with a for loop like this:
<?php foreach($posts as $post){
echo '<div class="post">'.$post.'</div>';
}
?>
where $posts hold all the body of the post.
or in the controler make something like this:
<?php foreach($posts as $post){
$html = $html + '<div class="post">'.$post.'</div>';
}
?>
then pass the $html to the view and display them.
Simple logic such as loops are acceptable in views, you don't want any business logic in there. Since the logic of this loop is for display purposes, I would use the first.
Generally in template files, the foreach: endforeach syntax is used.
<?php foreach($posts as $post):?>
<div class="post"><?php echo $post;?></div>
<?php endforeach;?>
Or if short tags are enabled (Please note that this is often disabled on production servers, but <?php cannot be disabled, so I'd recommend the above method):
<? foreach($posts as $post):?>
<div class="post"><?=$post?></div>
<? endforeach;?>
However this is a convention as opposed to a rule.
The first. The controller should never involve itself in presentation. If you have too much logic in a view, move it into a viewhelper/component/widget.
精彩评论