Welcome Guest, Not a member yet? Register   Sign In
bad design to put html in controller?
#1

[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?
#2

[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.
#3

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

[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.

#5

[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;




Theme © iAndrew 2016 - Forum software by © MyBB