Rendering elements from controllers

Published on December 21, 2007 and tagged with cakephp  controller  element  feature  view

If you work with Ajax you probably encountered situations when it would have been handy if you could render an element directly from the controller. But that was not possible. You had to use a view, and then from there you could render the element with renderElement().

In the meantime this feature has been added to the development branch (I don’t know when) as I learned today from a comment by gwoo.

If you have an element /app/views/elements/example.ctp in your application, then you can render it with the following code snippet from within your controller:

$this->render('/elements/example');

In the same way you can also render views which belong to other controllers.

27 comments baked

  • Vlad December 21, 2007 at 11:36

    Good hint!
    Too easy, too simple, but useful. ;-)

    Add this article to my tiddleWiki ;-)

  • dezpo December 21, 2007 at 11:42

    It doesn’t work for me. It still searching for views/controller/elements/example

  • majna December 21, 2007 at 12:23

    for 1,5 years i’m using
    $this->render(‘related’, ‘ajax’,ELEMENTS.’news’.DS.’related.thtml’);

    for rendering element form controller!?

  • Ritesh December 21, 2007 at 15:53

    hi,

    just before I saw your post, I posted the same question on google groups..now I got the solution thanks

  • cakebaker December 22, 2007 at 19:14

    @all: Thanks for your comments!

    @dezpo: This feature is currently only in the version from the development branch of the repository.

    @majna: Yes, that’s a possible way, even though I think the new solution is more elegant.

  • rtconner January 07, 2008 at 20:56

    Is it just me or don’t you want to use the following code, so that switching from windows to unix and back are not a problem.

    $this->render(DS.’elements’.DS.’example’);

  • cakebaker January 08, 2008 at 19:40

    @rtconner: Thanks for the hint, I fixed it in the article.

  • rtconner January 10, 2008 at 06:59

    Heh, well.. I’m not going to lie. I tried this today.. the DS thing did not work. I could not find the element. I had to do it the way you originally posted (‘/elements/name’). Sorry about that.

  • cakebaker January 16, 2008 at 12:20

    @rtconner: No problem, I changed it back ;-) And thanks for informing me!

  • wizualizacje February 06, 2008 at 23:57

    Thats good info. Thanks

  • Jeff February 13, 2008 at 01:03

    Hmm. This doesn’t seem to work for me. $this->render(“/elements/dir/element_name”); even with the forward slash at the beginning for some reason attempts to perpend the full path before “/elements/dir/element_name” rather than treating it as a relative path. I’m using 1.2. Anyone else have this problem?

  • cakebaker February 14, 2008 at 17:49

    @Jeff: Hm, here it works fine. What error message, if any, do you get?

  • dezpo March 07, 2008 at 10:50

    I think this was deprecated again :(

  • cakebaker March 09, 2008 at 17:45

    @dezpo: Why do you think it has been deprecated? At least here it works fine with the latest cake version from the development branch.

  • chanon April 14, 2008 at 18:42

    I’ve been using this but after updating my cake folder I can’t use it anymore either.

    What to use instead?

  • cakebaker April 14, 2008 at 18:53

    @chanon: Hm, which Cake version do you use? I just tested it with rev. 6659, and it seems to work fine here.

  • chanon April 14, 2008 at 19:01

    I’m on 6661 .. maybe it’s something in my code. I’ll try to make sure.

  • chanon April 14, 2008 at 19:20

    OK, I can confirm that it is not my code.

    The problem is that
    $this->render(‘/elements/example’);
    is now trying to find the file at
    /app/views/controllername/elements/example.ctp
    instead of
    /app/views/elements/example.ctp
    which is not how it used to be.
    (ex. like in your above article)

    This isn’t what I want as the element I want to render is common to many controllers.

  • chanon April 15, 2008 at 10:30

    I’ve found the workaround, just use the $file parameter:
    $this->render(null, ‘ajax’, VIEW . /elements/example.ctp);

  • cakebaker April 15, 2008 at 17:28

    @chanon: Strange, that it doesn’t work for you and that it works here. I have seen your ticket, and so I hope the devs can figure out what the cause of this issue is.

  • Wizualizacje 3d July 31, 2008 at 14:10

    I must try this.

  • Websites tagged "controllers" on Postsaver August 18, 2008 at 06:45

    [...] Controllers Prefer Springsteen And Sting Over George … saved by romanedirisinghe2008-08-13 – Comment on Rendering elements from controllers by Wizualizacje 3d saved by pmg1232008-08-10 – penguin united release the quad station for the wii controllers. saved [...]

  • Bookmarks about Elements September 21, 2008 at 15:45

    [...] – bookmarked by 4 members originally found by porkchop2f on 2008-09-06 Comment on Rendering elements from controllers by Websites tagged … http://cakebaker.42dh.com/2007/12/21/rendering-elements-from-controllers/#comment-103482 – [...]

  • Laurent December 18, 2009 at 20:17

    Hi!
    Your post is the closer I got to what i’m looking for: would you happen to know if i can reload an element from an ajax call within the element itself ? without reloading the whole view of course ?

    Maybe this is more clear: my view has thee elements: a photo, info about the photo, and comments. Once comments are added (via ajax), i’d like the controller to reload the comment element only.

    Sounds anal, but can’t find anything…
    Thx!!

  • cakebaker December 21, 2009 at 18:06

    @Laurent: I’m not sure I understand your use case correctly. But if you use the post function (assuming you are using jQuery) to post comments, then you could specify a callback function in which you can update the comments element if the post was successful.

    Does this help?

  • Laurent Kretz December 22, 2009 at 13:24

    It does, thanks.
    But my biggest point of misunderstanding was that I thought cake could by itself (on ajax call) update an element in a view.
    I understood that it returns the content, and that I have to update the element in the call back of the jQuery function.

    Thanks !

  • cakebaker December 22, 2009 at 18:06

    @Laurent: You are welcome :)

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License