views:

757

answers:

2

I think I might be approaching this in the wrong way, so I would appreciate any comments/guidance. Hopefully I can explain coherently enough what I am trying to achieve:

  • I want to create a block of HTML (e.g. a box containing a user's profile), which I will load as part of my layout on most pages that I generate.

  • I would also like to be able to re-generate the content within this box on its own from a separate URL. This is so I can update the box with an AJAX call.

  • I don't want to duplicate the code that creates this HTML.

I appreciate that I could initally load this box using an AJAX call, but that would seem to me to add an unnecessary call to the server?

The way I thought I could do it is by having a method in my controller that just renders this block of HTML, but how would I then request the output from this method within another controller / view?

How would you approach this?

Thanks in advance

+2  A: 

Create a view to generate the block of HTML for the user's profile and call it from your controller using:

$user_html = $this->load->view('user_view', $user_data, true);

The third parameter returns the view as a string instead of displaying it. This can then be passed into another view in the usual way.

$data['user_block'] = $user_html;
$this->load->view('page_view', $data);
foxy
However, this won't give you a seperate URL that you can use for an AJAX call.
naeblis
THanks. So I suppose the key thing here is to put all the processing of the data inside the model so I have as little code in the controller as possible.
Tom Haigh
And it doesn't reduce code duplication.
naeblis
I guess though if the bulk of code is in models and views, then controller isn't actually doing a lot, so it would be only a few lines of duplicated stuff
Tom Haigh
Copying and pasting the same code across all your controllers kinda ruins the point of using frameworks and OOP. However I should stfu as I don't have a better answer. Still thinking of a more elegant way to do this.
naeblis
The only code that would be duplicated across controllers would be about three lines to get the data from the model, pass it into the user_view and then send the returned html into the main view for the page.
foxy
As tomhaigh pointed out - all of the code that actually generates the HTML (the code you really don't want to be duplicated) is not duplicated.
foxy
A: 

As far as data is concerned, you dont 'need' to generate the HTML with PHP. You can use jQuery to generate the necessary elements from a JSON object output in the page source. You can then show a nice little 'loading' animation on the element whilst it gets around to hitting a state of 'domready'.

As far as repopulating a div is concerned, do the following:

Make a div for your profile and give it an id. Put each 'element' of data into it's own p element and wrap the actual data in a span element. e.g <p>Name: <span>Joe Bloggs</span></p> Then simply use jQuery to traverse the dom and repopulate the spans with data. Just make sure you output your data in the correct 'order'.

Any questions, feel free to feedback.

Jay
I see your point, but I would rather use javascript to progressively enhance a site rather than relying on it for things I should really be able to do server-side
Tom Haigh
Please give an example of the sort of html you would like to be generated dynamically.
Jay