CodeIgniter Forums
bad design to put html in controller? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: bad design to put html in controller? (/thread-54671.html)



bad design to put html in controller? - El Forum - 09-19-2012

[eluser]chubbypama[/eluser]
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?


bad design to put html in controller? - El Forum - 09-19-2012

[eluser]xerobytez[/eluser]
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.


bad design to put html in controller? - El Forum - 09-19-2012

[eluser]Rolly1971[/eluser]
why not use the table helper?


bad design to put html in controller? - El Forum - 09-20-2012

[eluser]chubbypama[/eluser]
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.




bad design to put html in controller? - El Forum - 09-20-2012

[eluser]ojcarga[/eluser]
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:

Code:
//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;