Quantcast

Rendering a view for ajax and non ajax

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Rendering a view for ajax and non ajax

realien
Hi guys,
 
I have a view where I need to render the same content when its an ajax call another time when its first page render.  I don't want a duplicate of the table_contents.thtml file even if its uses elements to create itself. So in the table_content.html view file has :
 
<div id='table_content'>
$this->render("table_contents");
</div>
 
and in the controller when I do an ajax update I do :
 
$this->render('table_contents', 'ajax');
 
the issue is when you render  a view inside a page causes it to render the view with its layout so it closes the html tag and the body tag at the end of the render call, and anything after is techinically invalid since the html tag has been closed. So my initial page render is actually wrong due to the table_contents being a view file and not an element file.
 
I know you can use elements for "content" which I do to actually create the details of the table_contents.thml but when the page loaded the first time its table_content div is the same as when its called from ajax and I hate having a duplicate view thml file.
 
Cheers
Grant
 
 
 

...........................................
› b l u e t u b e i n t e r a c t i v e.
.: grant davies
.: 404.428.6839 (c)
.: 708-983-1577 (F)
[hidden email]
http://www.bluetube.com/bti
A Tribal Chicken Designs Affiliate
 

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Rendering a view for ajax and non ajax

AD7six

Hi Grant,

It's probably wise to take a step back and look at your code with an
eye on what methods you should be using. You should not be calling
render in your view, or was that  typo? Your message implies that you
have table_contents.thtml and table_content.thtml, but I can´t really
make out what you have or want to achieve.

Regarding ajax and not: There is no need to create 2 views for the same
method, irrespective of the contents that is obviously a duplication.
Just include the requestHandler component - you then don't need to do
anything to have a layout for a normal request, and not for an ajax
request. Generally speaking, it's unnecessary for controller or view
code to be aware of whether it's an ajax call or not when processing a
request.

HTH,

AD7six
Please note:
The manual/bakery is a good place to start any quest for info.
The cake search (at the time of writing) erroneously reports less/no
results for the google group.
The wiki may contain incorrect info - read at your own risk (it's
mainly user submitted) :)
You may get your answer quicker by asking on the IRC Channel (you can
access it with just a browser here: http://irc.cakephp.org).

On Dec 4, 4:29 am, "Grant Davies" <[hidden email]> wrote:

> Hi guys,
>
> I have a view where I need to render the same content when its an ajax
> call another time when its first page render.  I don't want a duplicate
> of the table_contents.thtml file even if its uses elements to create
> itself. So in the table_content.html view file has :
>
> <div id='table_content'>
> $this->render("table_contents");
> </div>
>
> and in the controller when I do an ajax update I do :
>
> $this->render('table_contents', 'ajax');
>
> the issue is when you render  a view inside a page causes it to render
> the view with its layout so it closes the html tag and the body tag at
> the end of the render call, and anything after is techinically invalid
> since the html tag has been closed. So my initial page render is
> actually wrong due to the table_contents being a view file and not an
> element file.
>
> I know you can use elements for "content" which I do to actually create
> the details of the table_contents.thml but when the page loaded the
> first time its table_content div is the same as when its called from
> ajax and I hate having a duplicate view thml file.
>
> Cheers
> Grant
>
> ...........................................> b l u e t u b e i n t e r a c t i v e..: grant davies
> .: 404.428.6839 (c)
> .: 708-983-1577 (F)
>
> > [hidden email]
> >http://www.bluetube.com/bti<http://www.bluetube.com/bti>
> > A Tribal Chicken Designs Affiliate <http://www.tribalchicken.com/>
>
>  small.jpg
> 2KViewDownload


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

RE: Rendering a view for ajax and non ajax

realien

Sorry I should be more clear..

When the page is first rendered I have a table generated with content in it.

Once the page is up there are some selection boxes to allow you to filter the contents of the table.

So the first time the page is rendered there is no ajax call, its just rendered.

When the user selects a filter, I make an ajax call to have the table re-rendered without re-loading the page.

So my dilema..

My controller has the method  bureauPage which has a view bureau_page.thml   This has a div inside it

<results_table>
$this->render('results_table');
</results_table>

When its first rendered it needs to render the table which I could do with regular html calls or multiple calls to renderElement.

Now in my code when you apply the filter I need to update the results_table div  so in the controller I have

render('results_table', 'ajax');

So if I didn't user $this->render('result_table') in the view bureau_page.thml  I'd have to have to copies of the results_table php code, one when the page is first created, and one when I need to update the div due to the ajax call.

Can you tell me a better way to do this since I may just be going about it all wrong.

Cheers
Grant









...........................................
> b l u e t u b e i n t e r a c t i v e.
.: grant davies
.: 404.428.6839 (c)
.: 708-983-1577 (F)
> [hidden email]
> http://www.bluetube.com/bti


-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of AD7six
Sent: Monday, December 04, 2006 3:17 AM
To: Cake PHP
Subject: Re: Rendering a view for ajax and non ajax


Hi Grant,

It's probably wise to take a step back and look at your code with an eye on what methods you should be using. You should not be calling render in your view, or was that  typo? Your message implies that you have table_contents.thtml and table_content.thtml, but I can´t really make out what you have or want to achieve.

Regarding ajax and not: There is no need to create 2 views for the same method, irrespective of the contents that is obviously a duplication.
Just include the requestHandler component - you then don't need to do anything to have a layout for a normal request, and not for an ajax request. Generally speaking, it's unnecessary for controller or view code to be aware of whether it's an ajax call or not when processing a request.

HTH,

AD7six
Please note:
The manual/bakery is a good place to start any quest for info.
The cake search (at the time of writing) erroneously reports less/no results for the google group.
The wiki may contain incorrect info - read at your own risk (it's mainly user submitted) :) You may get your answer quicker by asking on the IRC Channel (you can access it with just a browser here: http://irc.cakephp.org).

On Dec 4, 4:29 am, "Grant Davies" <[hidden email]> wrote:

> Hi guys,
>
> I have a view where I need to render the same content when its an ajax
> call another time when its first page render.  I don't want a
> duplicate of the table_contents.thtml file even if its uses elements
> to create itself. So in the table_content.html view file has :
>
> <div id='table_content'>
> $this->render("table_contents");
> </div>
>
> and in the controller when I do an ajax update I do :
>
> $this->render('table_contents', 'ajax');
>
> the issue is when you render  a view inside a page causes it to render
> the view with its layout so it closes the html tag and the body tag at
> the end of the render call, and anything after is techinically invalid
> since the html tag has been closed. So my initial page render is
> actually wrong due to the table_contents being a view file and not an
> element file.
>
> I know you can use elements for "content" which I do to actually
> create the details of the table_contents.thml but when the page loaded
> the first time its table_content div is the same as when its called
> from ajax and I hate having a duplicate view thml file.
>
> Cheers
> Grant
>
> ...........................................> b l u e t u b e i n t e r
> a c t i v e..: grant davies
> .: 404.428.6839 (c)
> .: 708-983-1577 (F)
>
> > [hidden email]
> >http://www.bluetube.com/bti<http://www.bluetube.com/bti>
> > A Tribal Chicken Designs Affiliate <http://www.tribalchicken.com/>
>
>  small.jpg
> 2KViewDownload




--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Rendering a view for ajax and non ajax

AD7six

Hi Grant,

You should not be calling render in your view. To make reusable bits of
code, you /should/ make use of elements. I don´t quite follow what you
mean about multiple calls to renderElement, surely the element in this
context would contain either the form or the table, so at most there
would be 2 elements. Also, generally speaking, it's a bad idea to
specify ajax in a render call in a controller ($this->render(x,
'ajax');) That's what the request handler does for you automatically -
if someone accesses the url for your ajaxed controller action directly
(or doesn't have js enabled) they are not going to see a layout if you
hard code to use the ajax layout.

A different way of thinking would be to replace your $this->render in
your view with echo $ths->requestAction(UrlToDisplayResults); and
remove all controller logic for creating the table data from your
bureauPage action/controller.

The examples below might help (or perhaps some of the other
permutations):
http://www.noswad.me.uk/Pagination/Form
http://www.noswad.me.uk/Pagination/PRG/Ajaxed

You can get the source from the demos tab, note that for the second
example the view does not make use of elements but it should (in
keeping with what I said above).

Anyway, HTH or at least gives something to think about.

AD7six
Please note:
The manual/bakery is a good place to start any quest for info.
The cake search (at the time of writing) erroneously reports less/no
results for the google group.
The wiki may contain incorrect info - read at your own risk (it's
mainly user submitted) :) You may get your answer quicker by asking on
the IRC Channel (you can access it with just a browser
here:http://irc.cakephp.org).


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

RE: Rendering a view for ajax and non ajax

realien

Thanks, I ended up solving it using an element and removing the ajax setting from the render call, thanks for your help.

grant



...........................................
> b l u e t u b e i n t e r a c t i v e.
.: grant davies
.: 404.428.6839 (c)
.: 708-983-1577 (F)
> [hidden email]
> http://www.bluetube.com/bti


-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of AD7six
Sent: Tuesday, December 05, 2006 3:18 AM
To: Cake PHP
Subject: Re: Rendering a view for ajax and non ajax


Hi Grant,

You should not be calling render in your view. To make reusable bits of code, you /should/ make use of elements. I don´t quite follow what you mean about multiple calls to renderElement, surely the element in this context would contain either the form or the table, so at most there would be 2 elements. Also, generally speaking, it's a bad idea to specify ajax in a render call in a controller ($this->render(x,
'ajax');) That's what the request handler does for you automatically - if someone accesses the url for your ajaxed controller action directly (or doesn't have js enabled) they are not going to see a layout if you hard code to use the ajax layout.

A different way of thinking would be to replace your $this->render in your view with echo $ths->requestAction(UrlToDisplayResults); and remove all controller logic for creating the table data from your bureauPage action/controller.

The examples below might help (or perhaps some of the other
permutations):
http://www.noswad.me.uk/Pagination/Form
http://www.noswad.me.uk/Pagination/PRG/Ajaxed

You can get the source from the demos tab, note that for the second example the view does not make use of elements but it should (in keeping with what I said above).

Anyway, HTH or at least gives something to think about.

AD7six
Please note:
The manual/bakery is a good place to start any quest for info.
The cake search (at the time of writing) erroneously reports less/no results for the google group.
The wiki may contain incorrect info - read at your own risk (it's mainly user submitted) :) You may get your answer quicker by asking on the IRC Channel (you can access it with just a browser here:http://irc.cakephp.org).




--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Loading...