CodeIgniter Forums
Opinions Wanted: Projects organization for AJAX - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Opinions Wanted: Projects organization for AJAX (/showthread.php?tid=2151)



Opinions Wanted: Projects organization for AJAX - El Forum - 07-18-2007

[eluser]beyondwords[/eluser]
I am using a fair amount of AJAX in my project (information management for a medium-sized non-profit).

Currently I am using Prototype/Script.aculo.us for the AJAX functionality.

I would like to know how people organize their PHP/HTML bound to be returned through an AJAX.Request object.
Currently i have something like:

Code:
$return_str ='';
        if ($query->num_rows() > 0)
        {
            $return_str .= "<table width=\"100%\">\n";
            ...
            foreach ($query->result() as $row)
            {
                ...
                $return_str .= "<tr id=\"some_$some_id\">\n<th>$row->some_name</th>\n";
(where ... is omitted code for the sake of brevity).

For the brief time I used Rails, there was a function (render :partial => 'name_of_file') similar to a php include that would allow me to send bits of rhtml (ruby-enhanced html) back as the response to an AJAX.Request.

So far I have been using the technique above of feeding everything to a string manually. My impression is that neither File Helper read_file() function nor a straight include will quite cut it. (I haven't exhausted these options because I have more to do than time in which to do it... so for the moment I'm chugging along with what I have).

So my question:
Is there a way to render a bunch of PHP-laced HTML and send it back as the response to an AJAX.Request?

Thanks in advance for your responses!


Opinions Wanted: Projects organization for AJAX - El Forum - 07-19-2007

[eluser]Phil Sturgeon[/eluser]
[quote author="beyondwords" date="1184820379"]Is there a way to render a bunch of PHP-laced HTML and send it back as the response to an AJAX.Request?

Thanks in advance for your responses![/quote]

Use views? 8-/


Opinions Wanted: Projects organization for AJAX - El Forum - 07-19-2007

[eluser]beyondwords[/eluser]
Damn I feel foolish now. For some reason I was so convinced it wouldn't work that I never tried.

Thanks for setting me straight.


Opinions Wanted: Projects organization for AJAX - El Forum - 07-19-2007

[eluser]esra[/eluser]
Think of having a master view or template which is composed of containers. Each container can have its own CSS id. Each container could have an embedded PHP variable which would be a placeholder for embedding a view fragment. Then you can use AJAX techniques to show and hide the containers which act as placeholders for positioning the view fragments.

For the past month, I have been using EXT JS in combination with Coolfactor's View library, but it works well with other AJAX/Widget libraries as well. If you read the forum thread on Coolfactor's View library and think of his part method as a solution for assembling multiple views into a PHP template (master view), you should get the general idea. Once you understand how a master view can be composed of multiple view fragments, using Ajax approaches for showing and hiding view fragments becomes easier to visualize.

There are other solutions mentioned on the forums other than using Coolfactor's View library. Some solutions use PHP-only coding solutions (such as Coolfactor's solution) and other use small to large template parsing engines. What you use to construct your views is probably going to boil down to what you or your end users feel most comfortable with.


Opinions Wanted: Projects organization for AJAX - El Forum - 07-19-2007

[eluser]beyondwords[/eluser]
Thanks for weighing in esra.

Once thepyromaniac pointed out that I could just feed views back directly to the AJAX.Request I started doing more or less what you describe (main file loading in separate views with PHP/HTML data for the given section/block).

Thanks!