CodeIgniter Forums
HTML in Model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: HTML in Model (/showthread.php?tid=74457)



HTML in Model - AngelTrader - 09-25-2019

Hi Everyone

New to MVC Model programming, so basic question sorry : 

Normally, if I understand correctly, this is where everything resides : 

Controller : Loads the different logic and views
Model : contains the logic (ie loading data from MySQl, different functions for calculating, etcetera)
View : Contains the HTML markup and little PHP to ech values received from the Model

This is all pretty straightforward until you run into AJAX to recalculate certain parts of your page.
Let's say I have a table with users, when I add a user I want to reflect that in the table without refreshing the whole page.
So normally you would run an AJAX call to some PHP script that then renders the table again.

Under the MVC model, my AJAX call is done to model/function , but that effectively means that part of my HTML markup
will have to be moved to the Model, because the return value to the AJAX call has to contain all the HTML in order to put it in the response DIV . And this is my question : is it normal and OK to move parts of HTML to the model

Thanks, hopefully it's a bit clear what I'm trying to explain.


RE: HTML in Model - ciadmin - 09-25-2019

Noooooooooo - don't do it!
What you are looking for would be to have the AJAX request expect JSON data back from the server-side controller, which referenced the appropriate model. You would then format/mangle/render that JSON data client-side.
So, no it is not normal & ok, IMHO. Your original (bolded) perspective makes the most sense!


RE: HTML in Model - AngelTrader - 09-25-2019

I agree, and it was my gut feeling that no HTML should be in model .... unfortunately I have no idea where to start since when I call the model/function , it will return only JSON data, then where do I get my formatting from.

Normally when you run an AJAX call you have the "success" handler in which you just print the result like $('#mydiv').html(data) where "data" contains the full rendered HTML with values , but since I can't use HTML in the model , where do I get that from then.

Sorry, i'm totaly confused


RE: HTML in Model - dave friend - 09-26-2019

(09-25-2019, 11:35 PM)AngelTrader Wrote: I agree, and it was my gut feeling that no HTML should be in model .... unfortunately I have no idea where to start since when I call the model/function , it will return only JSON data, then where do I get my formatting from.

Normally when you run an AJAX call you have the "success" handler in which you just print the result like $('#mydiv').html(data) where "data" contains the full rendered HTML with values , but since I can't use HTML in the model , where do I get that from then.

Sorry, i'm totaly confused

Most often the AJAX is returning data to an already existing page of HTML and the success function uses the return to modify what's there. So the answer to "where do I get my formatting from" is that it's either already there, or the success function will create it by concatenating string literals and the returned data before injecting the results into the DOM.

It's not true that you "can't use HTML". You most certainly can, but you should adjust the $.ajax options accordingly. AJAX is seen as a way to offload presentation work to the client-side and JSON make great sense for the data format. But sometimes is just plain easier to send back HTML markup that's ready for display.

You can make $this->load->view('something') to return blocks of HTML to ajax.success. Read the docs again looking for the example on how to have view() return a string instead of outputting directly.


RE: HTML in Model - albertleao - 09-26-2019

NOP

Models should contain business logic that is related to the DB table the model is associated with

Controllers should get data from models and/or libraries and pass them to the view

Views should have HTML code.

HTML code should never be in your controllers MUCH LESS your models.


RE: HTML in Model - ciadmin - 09-26-2019

I am not thinking HTML in any controller, but having the controller build view fragments to return.
I think we are on the same page,


RE: HTML in Model - AngelTrader - 09-26-2019

I've created a seperate view for the table structure.
I then created a seperate function in the controller that loads the data in the view and returns it as a string (byu appending 'true' at the load view function).

I then return the full HTML string back to the AJAX success function.

Works like a charm, thank you everyone !!