Rendering in models

Hello forum,

I’ve got a question - why it is a bad idea to render a partial view via a model method?

I know that its generally accepted that you don’t render in a model of an MVC pattern. I always wondered why.

Your model should not be concerned with how data is presented. That’s what the presentation layer (your views) is for.

Because if you do so you create a huge mess which is hard to deal with.

Patrick, in this case isn’t the partial view concerned with the data presentation?

Sam, what kind of mess you’re talking about? All of the view files of a model are in the same directory. Could you please provide some real life examples?

Not quite. If the model is "ordering" the rendering, then the model is concerning itself with data presentation.

One of the ideas behind MVC is that your data presentation is independent of the business logic, and vice-versa.

That allows you, for example, to make significant changes to how you show information to the user, without making any changes to your model(s).

The same goes the other way in that your views don’t bother with business logic and, for example, don’t query the database. Otherwise, as Sam pointed out, you’ll find yourself in a big mess.

If you are interested in a real-life example, maybe you could post your example code?

I can’t grasp the following:

If there is no data in the business logic, there would be no data to show.

So here is a badly written example on a Person model:

    public function getSimpleInfo($s = '<br>', $style = true, $links_and_icons = true) {

        $return Yii::$app->controller->renderPartial('/person/_simple_info', [

            's' => $s,

            'style' => $style,

            'links_and_icons' => $links_and_icons,

            'person' => $this



In this case instead of calling renderPartial and passing all of the variables in a view, I could just call $person->getSimpleInfo(…);

Isn’t this easier to deal with? And what are the chances that it could create a mess? Sorry for asking that kind of dumb questions, but I learn and approach things in a different manner in order to grasp the idea.

Slight difference: "data" does not equal "data presentation". Your model is of course full of data. But data presentation is what views are for.

Who is calling that function? Is it another model, a controller, a view?

Many developers struggle to grasp the idea, but way too few ask questions. And instead create huge messes. So it’s good to see that you are asking ;)

A controller, but it is in a method of an model class.

Thanks on supporting this cause :D

OK then why do you take the "detour" of rendering in a model method?

Why not just have this piece of code directly in your controller?

return $this->renderPartial('/person/_simple_info', [

    's' => '<br>',

    'style' => true,

    'links_and_icons' => true,

    'person' => $person


Because the controller renders the whole view, not the partial view.

If not “messless”, at least less mess ;) to pass parameters to render(partial).

Model not dependent on view, view not dependent on (exact) model.

Then how about letting the view render the partial view?