我不确定这样的回调是否存在特定于“元素”,但查看源代码,使用与视图本身相同View::element()
的方法呈现元素,并且应该触发和 _render()
beforeRender()
afterRender()
创建自定义视图类和自定义回调
您可以使用自定义的“视图”类并覆盖该element()
方法,例如在助手中触发您自己的“自定义”回调
像这样的东西;
应用程序/视图/app_view.php
class AppViewView extends View {
/**
* custom 'element()' method, triggers custom
* before/aferRenderElement callbacks on all loaded helpers
*/
public function element($name, $params = array(), $loadHelpers = false)
{
$this->_triggerHelpers('beforeRenderElement');
$output = parent::element($name, $params, $loadHelpers);
$this->_triggerHelpers('afterRenderElement');
}
/**
* Names of custom callbacks
*/
protected $_customCallBacks = array(
'beforeRenderElement',
'afterRenderElement',
);
function _triggerHelpers($callback)
{
if (!in_array($callback, $this->_customCallbacks)) {
// it's a standard callback, let the parent class handle it
return parent::_triggerHelpers($callback);
}
if (empty($this->loaded)) {
return false;
}
$helpers = array_keys($this->loaded);
foreach ($helpers as $helperName) {
$helper =& $this->loaded[$helperName];
if (is_object($helper)) {
if (
is_subclass_of($helper, 'Helper')
&& method_exists($helper, $callback)
) {
$helper->{$callback}();
}
}
}
}
}
然后,在您的 AppController 中指定要使用的“视图”类;
class AppController extends Controller {
public $view = 'AppView';
}