php CakePHP 视图包括其他视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/93632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:25:51  来源:igfitidea点击:

CakePHP View including other views

phptemplatescakephp

提问by Fernando Barrocal

I have a CakePHP application that in some moment will show a view with product media (pictures or videos) I want to know if, there is someway to include another view that threats the video or threats the pictures, depending on a flag. I want to use those "small views" to several other purposes, so It should be "like" a cake component, for reutilization.

我有一个 CakePHP 应用程序,它在某些时候会显示带有产品媒体(图片或视频)的视图,我想知道是否有其他视图可以威胁视频或威胁图片,具体取决于标志。我想将那些“小视图”用于其他几个目的,因此它应该“像”一个蛋糕组件,以进行重用。

What you guys suggest to use to be in Cake conventions (and not using a raw include('')command)

你们建议在 Cake 约定中使用什么(而不是使用原始include('')命令)

回答by Paolo Bergantino

In the interest of having the information here in case someone stumbles upon this, it is important to note that the solution varies depending on the CakePHP version.

为了防止有人偶然发现这里的信息,请务必注意解决方案因 CakePHP 版本而异。

For CakePHP 1.1

对于 CakePHP 1.1

$this->renderElement('display', array('flag' => 'value'));

$this->renderElement('display', array('flag' => 'value'));

in your view, and then in /app/views/elements/you can make a file called display.thtml, where $flagwill have the value of whatever you pass to it.

在您的视图中,然后/app/views/elements/您可以创建一个名为 的文件display.thtml,其中$flag将包含您传递给它的任何值。

For CakePHP 1.2

对于 CakePHP 1.2

$this->element('display', array('flag' => 'value'));

$this->element('display', array('flag' => 'value'));

in your view, and then in /app/views/elements/you can make a file called display.ctp, where $flagwill have the value of whatever you pass to it.

在您的视图中,然后/app/views/elements/您可以创建一个名为 的文件display.ctp,其中$flag将包含您传递给它的任何值。



在这两个版本中,元素都可以访问视图可以访问的所有数据+您传递给它的任何值。此外,正如有人指出的那样,requestAction()requestAction()这也是一种选择,但如果不使用缓存,它会严重影响性能,因为它必须执行正常操作的所有步骤。

回答by Chris Hawes

In your controller (in this example the posts controller).

在您的控制器中(在本例中为帖子控制器)。

function something() {
    return $this->Post->find('all');
}

In your elements directory (app/views/element) create a file called posts.ctp.

在您的元素目录 (app/views/element) 中创建一个名为 posts.ctp 的文件。

In posts.ctp:

在posts.ctp中:

$posts = $this->requestAction('posts/something'); 
foreach($posts as $post): 
    echo $post['Post']['title']; 
endforeach; 

Then in your view:

那么在你看来:

<?php echo $this->element('posts'); ?>

This is mostly taken from the CakePHP book here: Creating Reusable Elements with requestAction

这主要取自这里的 CakePHP 书: Creating Reusable Elements with requestAction

I do believe that using requestAction is quite expensive, so you will want to look into caching.

我确实相信使用 requestAction 非常昂贵,因此您需要研究缓存。

回答by LenArt

Simply use:

只需使用:

<?php include('/<other_view>.ctp'); ?>

in the .ctp your action ends up in.

在 .ctp 中,您的操作最终会出现。

For example, build an archived function

例如,构建一个归档函数

function archived() {
  // do some stuff
  // you can even hook the index() function
  $myscope = array("archived = 1");
  $this->index($myscope);
  // coming back, so the archived view will be launched
  $this->set("is_archived", true); // e.g. use this in your index.ctp for customization
}

Possibly adjust your index action:

可能调整您的索引操作:

function index($scope = array()) {
  // ...
  $this->set(items, $this->paginate($scope));
}

Your archive.ctp will be:

您的 archive.ctp 将是:

<?php include('/index.ctp'); ?>

Ideal reuse of code of controller actions and views.

控制器操作和视图代码的理想重用。

回答by Rob Forrest

For CakePHP 2.x

对于 CakePHP 2.x

New for Cake 2.x is the abilty to extend a given view. So while elements are great for having little bits of reusable code, extending a view allows you to reuse whole views.

Cake 2.x 的新功能是扩展给定视图的能力。因此,虽然元素非常适合拥有少量可重用代码,但扩展视图允许您重用整个视图。

See the manual for more/better information

有关更多/更好的信息,请参阅手册

http://book.cakephp.org/2.0/en/views.html#extending-views

http://book.cakephp.org/2.0/en/views.html#extending-views

回答by Lucas Oman

Elements work if you want them to have access to the same data that the calling view has access to.

如果您希望元素可以访问调用视图可以访问的相同数据,则元素会起作用。

If you want your embedded view to have access to its own set of data, you might want to use something like requestAction(). This allows you to embed a full-fledged view that would otherwise be stand-alone.

如果您希望您的嵌入式视图能够访问其自己的数据集,您可能需要使用类似requestAction(). 这允许您嵌入一个完整的视图,否则该视图将是独立的。

回答by Javier Constanzo

I want to use those "small views" to several other purposes, so It should be "like" a cake component, for reutilization.

我想将那些“小视图”用于其他几个目的,因此它应该“像”一个蛋糕组件,以进行重用。

This is done with "Helpers", as described here. But I'm not sure that's really what you want. The "Elements" suggestion seems correct too. It heavily depends of what you're trying to accomplish. My two cents...

这是通过“Helpers”完成的,如here所述。但我不确定这真的是你想要的。“ Elements”建议似乎也正确。这在很大程度上取决于您要实现的目标。我的两分钱...

回答by Alex Solovyh

In CakePHP 3.x you can simple use:

在 CakePHP 3.x 中,您可以简单地使用:

$this->render('view')

This will render the view from the same directory as parent view.

这将从与父视图相同的目录呈现视图。