Best Practice coding View

I understand that in View we should not put something too complex. It should be put it in the Controller.

Now I have a case that I don’t know what approach is a best practice. I create a complex table, it’s kinda like a Pivot table…

The problem is the table is too large, the cell’s value is too many. I don’t think store the data to array and pass when calling render() method is a good one. But putting the source code to process the cell’s value in the view is making the View’s code ugly too. I don’t know what approach is better. What should I do? Which one should I choose, or is there any way?

By the way, in a good practice way, may I calling function in view??

What I tend to do is create static classes (Helpers) to help cut down on the amount of code written in the view.

A silly example:


	public static function link_to_user($name, $id) {

    	return CHtml::link(ucfirst($name),array('/user/user/view', "id" => $id));

	}



I can now do this in the view:


<?php echo MyHelper::link_to_username($model->username, $model->id); ?>

Yeah, currently I am using this method too… But one drawback is, when there are too many function in one static class, it might become confusing. But for this time being, guess this is the best approach…

If it’s not really used all over the place, I tend to put output utility functions in the controller, and the model if it queries.

That depends.

But, like you, I am always looking for cleaner ways. ;)

what about widgets?

Mbi, you’re right!

I just tested widgets, and they are going to be my new favourite method of keeping the views clean. :D

Am going to convert most of my ‘helper’ functions to widgets instead.

I like how they combine logic and presentation (own views).

Yeah I sometime forget there is widget this thing

But still, sometimes I got some case that widget seems like cannot help too.

One of the problem is echoing table

for example

I want to show a table with the cell colspan’s value is 2 if the value is bla bla

and the colspan value is 3 if the value is bla bla. If inside one table there are lot

of conditions like this, it just make the view looked uglier. Moreover with the

foreach looping. What make it looked ugly is the code of HTML must mixed with php which is not

pretty for me…

If you have some ugly code in your view, then just wrap it up and hide it in a widget.

A widget can have it’s own views, so you could tuck the conditional code in that/these.

That’s what I do now. :lol: