Yii Framework Forum: Best Practice coding View - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Best Practice coding View when there are huge data need to be displayed Rate Topic: -----

#1 User is offline   junxiong 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 541
  • Joined: 21-June 10

Posted 17 November 2010 - 09:32 AM

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??
“The most likely way for the world to be destroyed, most experts argue, is by accident. That’s where we come in; we’re computer professionals. We cause accidents.” - Nathaniel Borenstein

Yii Playground : Collaborative demo apps. You can join to improve it too!
My Team's Blog: In Indonesian.
0

#2 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 17 November 2010 - 10:56 AM

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); ?>

"Less noise - more signal"
0

#3 User is offline   junxiong 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 541
  • Joined: 21-June 10

Posted 18 November 2010 - 01:19 AM

View Postjacmoe, on 17 November 2010 - 10:56 AM, said:

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...
“The most likely way for the world to be destroyed, most experts argue, is by accident. That’s where we come in; we’re computer professionals. We cause accidents.” - Nathaniel Borenstein

Yii Playground : Collaborative demo apps. You can join to improve it too!
My Team's Blog: In Indonesian.
0

#4 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 18 November 2010 - 07:18 AM

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. ;)
"Less noise - more signal"
0

#5 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 604
  • Joined: 08-May 09

Posted 18 November 2010 - 08:04 AM

what about widgets?
2

#6 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 19 November 2010 - 03:12 AM

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).
"Less noise - more signal"
0

#7 User is offline   junxiong 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 541
  • Joined: 21-June 10

Posted 19 November 2010 - 10:40 AM

View Postjacmoe, on 19 November 2010 - 03:12 AM, said:

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....
“The most likely way for the world to be destroyed, most experts argue, is by accident. That’s where we come in; we’re computer professionals. We cause accidents.” - Nathaniel Borenstein

Yii Playground : Collaborative demo apps. You can join to improve it too!
My Team's Blog: In Indonesian.
0

#8 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 19 November 2010 - 12:17 PM

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:
"Less noise - more signal"
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users