Where should "display functions" live in an MVC web app?

Posted by User on Programmers See other posts from Programmers or by User
Published on 2012-09-27T23:27:15Z Indexed on 2012/09/28 3:49 UTC
Read the original article Hit count: 241

Filed under:
|
|
|

I'm using the Yii Framework which is an MVC php framework that is pretty similar to your standard web-based MVC framework. I want to display the related data from a many-to-many table as a list of strings in my view.

Assuming a table schema like:

tag { id, name }
post { id, title, content, date }
post_tag { post_id, tag_id }

A post will display like:

Date: 9/27/2012 
Title: Some Title
Content: blah blah blah...
Tags: Smart Funny Cool Informative

I can achieve this by doing something like this in my Post view:

<?php
  echo join(' ',
           array_map(function($tag) { return $tag->name; }, $model->tags));
?>

(where $model->tags is an array of Tag objects associated with my model)

My questions are:

  1. Is this amount of code/logic okay in the view? (Personally I think I'd rather just reference a property or call a single function.)
  2. If not, where should this code live? In the model? the controller? a helper?

Potentially I may want to use in in other views as well. Ultimately I think its purely a display issue which would make me think it should be in the view, but then I have to repeat the code in any view I want to use it in.

© Programmers or respective owner

Related posts about php

Related posts about mvc