Rendering elements from controllers

Published on 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:


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

27 comments baked

  • Vlad

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

    Add this article to my tiddleWiki ;-)

  • dezpo

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

  • majna

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

    for rendering element form controller!?

  • Ritesh


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

  • cakebaker

    @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

    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.


  • cakebaker

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

  • rtconner

    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

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

  • wizualizacje

    Thats good info. Thanks

  • Jeff

    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

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

  • dezpo

    I think this was deprecated again :(

  • cakebaker

    @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

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

    What to use instead?

  • cakebaker

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

  • chanon

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

  • chanon

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

    The problem is that
    is now trying to find the file at
    instead of
    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

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

  • cakebaker

    @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

    I must try this.

  • Websites tagged "controllers" on Postsaver

    […] 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

    […] – bookmarked by 4 members originally found by porkchop2f on 2008-09-06 Comment on Rendering elements from controllers by Websites tagged … – […]

  • Laurent

    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…

  • cakebaker

    @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

    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

    @Laurent: You are welcome :)

© daniel hofstetter. Licensed under a Creative Commons License