开发者

storing a rendered element in a variable in CakePHP

I am having some trouble trying to "capture" the rendered html of an elmenet in cake php.

Say I have an element named "message.ctp"

I would like to do something like the following:

A making a $.getJSON request to an action in a controller say jso开发者_高级运维nAction(). Within this action I will perform some DB updates and return a json string. I would like to store the html is a part of the json object. Doable?

function jsonAction() {
    //Do DB update
    if(db update was ok) {
        $response = array("completed" => true, "html" => $this->render("message"));
    } else {
       $response = array("completed" => false);
    }
    echo json_encode($response);
}

What seems to be happening right now is that the render method echos the rendered value instead of returning it.

Anyway I can achieve this?

Thanks

Regards Gabriel


Forget elements for the time being.

First of all you have to separate everything that includes outputting stuff from the controller (either it is HTML or JSON or whatever).

For every controller action you have to have a corresponding view. So, for controller action jsonAction you should have a view names json_action.ctp (at the corresponding folder, e.g. if jsonAction is at MessagesController create a folder named /view/messages/json_action.ctp).

Set your variable from controller, display it at view and you are done. Do not forget to $this->layout = 'empty' from controller so that you display only what you have at the view.

Generally you should redo the CakePHP tutorials and reread the book it order to understand better the MVC (Model-View-Controller) pattern and CakePHP structure.


Do you mean this?

$myRenderedHtml = $this->element('message');
                         ^^^^^^^
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜