Keeping things MVC-ish

Hello,

So I have a loop that I’m running. And within that loop, I currently have a function that changes the date format from the database, into a more readable date, such as “Posted 2 days ago”.

I’m doing the change within the loop. I’m wondering if anyone knows a way to do all changes within the Model instead of the page itself?

Example:


<?php foreach($this->homePosts() as $home):
  $date = $this->dateToWords($home['date']);
?>
  <div class="posterName"><?php echo $home['username']; ?></div>
  <div class="posterMessage"><?php echo $home['message']; ?></div>
  <div class="posterDate"><?php echo $date; ?></div>
<?php endforeach; ?>

You might think this is not that bad, but as I add more restrictions, this gets worse and worse.

Example:


<?php foreach($this->homePosts() as $home):
  $date = $this->dateToWords($home['date']);
  $username = strlen($home['username']) > 20 ? substr($home['username'], 0 , 20) : $home['username'];
  $message = strlen($home['message']) > 1000 ? substr($home['message'], 0, 1000).'...read more' : $home['message'];
?>
  <div class="posterName"><?php echo $username; ?></div>
  <div class="posterMessage"><?php echo $message; ?></div>
  <div class="posterDate"><?php echo $date; ?></div>
<?php endforeach; ?>

I’m trying to move as much functionality as I can out of the Views and leaving it as simple as I can. Hope someone can help! Thanks :slight_smile:

is this view or model?
i am not 100% sure whats the problem here
but this is what is done in cake php a mvc framework
it has helper call time
var $helpers=array(‘Time’,‘Others’);
then in view we do
<?php echo $time->niceShort($array_key[‘Modelname’][‘Variable’]) ?>
so like wise may be u can define some function like niceShort in model ,pass array to view and use that function in similar function…

just some clues…sorry if i misunderstood…
using $this in view is not recommended if you ask me…

What you see is the view.

I guess passing an array into another array would do it. But I was hoping for something different, I guess.

I use $this because it’s available as I do a require_once within a controller. If I don’t use $this I would need to create a new object of that class.

Why do you say it’s “not recommended” within the view?

well if it is view then i dont see much problems with that except
storing $date in a variable doesnt seem to have any specific benefit here
(will reduce a bit of code …may be negligible for now)

i say using $this in view is not recommened as theoritically it voilates the main notion of V of MVC…
V is for designers(in theory),a php programmer may not work in it (i know it is easier said than done…) and designer may not have any idea what $this stand for …either he should use $self or $this…what $this refers at a instance…or she may not know what $this is…designer is suppose to know css not php…lol

so at that time programmer will have to make changes to design…
then why to separate files (or even think separately) and make things difficult…if it is to be done finally in same old way…

so i think view should be least programming knowledge requiring as possible…theory says so …but i aslo know it is easier said than done and even now a designer need to be half programmer if he wish to work on mvc…
thats why i say “recommend” not “should” or “must”…lol