CakePHP:如何在控制器内使用视图元素

2021-12-21 00:00:00 php cakephp cakephp-2.0

我正在尝试弄清楚如何在控制器内使用我的视图元素之一...

I'm trying to figure out how to use one of my view elements inside of a controller...

我知道,我知道:不要那样做!"(99% 的情况下这是正确答案)

I know, I know: "Don't do that!" (99% of the time this is the correct answer)

但我认为我确实有充分的理由.该操作正在处理返回标记的 AJAX 请求.返回的标记是一个列表,我使用元素在其他地方显示它.所以为了保持我的代码干燥,我认为在这里这样做是合适的.

But I think I actually have a good reason. The action is handling an AJAX request which returns markup. The returned markup is a list which I display everywhere else using an element. So in an effort to keep my code DRY, I think it's appropriate to do this here.

这可能吗?

推荐答案

简单:

$view = new View($this, false);
$content = $view->element('my-element', $params);

还有:

不要再这样做了!!!

相关文章