2010年6月3日星期四

[fw-mvc] Cloning the view required for partial view helper?

I remember way back in the day (when we were all much younger) using the "action" view helper was discouraged because it was expensive. This makes sense -- it involves dispatching a new request, invoking all the the plugins, helpers, etc.

Instead, many suggested to use partials -- or better yet, calling Zend_View#render() for the best performance.

According to the ZF Performance Guide[1], partials are discouraged because they, too, can be expensive. The main argument against partials is the need to clone the view. I can agree that cloning the view is definitely slower than not cloning it. My question is, would it be possible to update the partial helper so that it does not need to clone the view?

For example, something like this (simplified) seems like it would work:

public function partial(/* ... /*)
{
    // Keep track of original view vars
    $oldVars = $this->view->getVars();

    // Clear vars and assign new vars from $model
    $this->view->clearVars();
    $this->view->assign($model);

    // Use built-in render
    $partial = $this->view->render(/* ... */);

    // Restore original vars
    $this->view->clearVars();
    $this->view->assign($oldVars);

    return $partial;
}

Of course this is just a dumbed-down version of the existing helper, but I wanted to point out how I believe it could be possible to improve the performance of partial by not cloning the view. What do you think?

I double-checked Zend_View and Zend_View_Abstract, and there isn't a __clone() method so I don't think we'd be missing out on any magic that cloning the view could provide.


--
Hector

没有评论: