kentverger
kentverger

Reputation: 475

what could be better for respect the MVC?

what 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.

Upvotes: 1

Views: 118

Answers (2)

Gazler
Gazler

Reputation: 84190

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.

Upvotes: 4

blockhead
blockhead

Reputation: 9705

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.

Upvotes: 1

Related Questions