• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bad design to put html in controller?

Hi there. i'm writing some functions / methods in my controller that are going to be ajax calls. Using the data returned by the function, I ultimately need to create a table.
But I don't want to have to write javascript to do this.
I'd rather just send a string back that represents a table and all the data inside.

But is this considered bad design because I have html in the controller instead of in the view?

Yes it can be considered bad practice, you want to go for complete separation of data, logic and views. Data being your models, logic being your controllers and views being what gets sent to your users browsers. I would place the table html in a view file and then load the table view so it returns the result, then you can take this and use it in your main page. Will be cleaner and your controller won't seem cluttered with HTML.

why not use the table helper?

That's a good suggestion Rolly1971. I didn't know about that particular library. I'll look into it. Thanks!
Thanks xerobytez for your input as well. Much appreciated.

Following the xerobytez's suggestion why don't you just create a view (table_ajax.php) and from the controller you do something like this:

//data will have al the info to create the table in the view file
$data = array();
//With the third parameter (true) it returns a string with the content of the view file
$string_table = $this->load->view("table_ajax", $data, true);
return $string_table;

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.