Welcome Guest, Not a member yet? Register   Sign In
functions in views
#1

[eluser]Unknown[/eluser]
Hello,

I hear a lot of people banging on about how calling functions inside views is bad practice.

What if I had a foreach loop that echo'd out some data but wanted to call a function with some of that data. e.g

<?php foreach ($posts as $post) {

echo $post->title;
echo $post->content;
time_difference($post->timestamp);

}

?>

Notice the time_difference function? How would I do that without calling it in the function?
#2

[eluser]Aken[/eluser]
If all your time_difference() function does is take the timestamp and turn it into something like a human-friendly time, then you're fine. That isn't a big deal, because it's a presentational thing. You'll get some people who strictly say NO, but it's not a hard-written rule.

Just don't go doing a bunch of logic stuff in your views, that's what you should be avoiding.




Theme © iAndrew 2016 - Forum software by © MyBB