cakephp - 从外部 ctp 文件中获取 block

标签 cakephp view block cakephp-2.1

我读到thisthis 。 但我找不到答案。

我有这个文件,其中存在我的操作 View : View/MyController/index.ctp

我也有这个文件 查看/Commons/blocks.ctp

Blocks.ctp 文件包含以下内容:

$this->start('sidebar1');
echo 'Some content';
$this->end();

$this->start('sidebar2');
echo 'Some content more';
$this->end();

因此,在“index.ctp”文件中,我想获取sidebar1sidebar2。 我怎样才能做到这一点? 我将其写入index.ctp,但没有工作。

<?php echo $this->fetch('sidebar1'); ?>

而且这个也不起作用

<?php echo $this->fetch('../View/Commons/blocks.ctp/sidebar1'); ?>

谢谢

最佳答案

放在你的index.ctp的顶部:

$this->extend('Common/blocks.ctp');

通过 block 和 View 继承,您可以创建基本上类似于标准 Cake 布局文件的“子布局”。因此,您将拥有主layout.ctp,并且 Controller 操作 View ctp将基于通过 block “填充”的父 View 文件(例如/Commons/xxxx.ctp)。

block 类似于元素,但不太“正式”,除非您使用“ View 继承”功能。它们的标记和数据是在您的脚本中创建的,可能在多个位置,因此它们的调试/维护可能会更加麻烦(即想象将标记附加到来自多个类的 block )。如果不使用继承,它们也很难重用。

元素更像是独立的 View 文件,可以在任何 Controller +操作 View 或布局中使用:所有标记都在一个位置,您只需传递数据。

底线:如果您是 Cake 新手,您只需使用元素就可以顺利完成。 View 继承可以帮助使 View /布局更加优雅,但代价是增加一些复杂性。

关于cakephp - 从外部 ctp 文件中获取 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10113765/

相关文章:

ios - 如何将 objective-c block 存储到 NSDictionary 中?

ruby - 使用枚举器更改 ruby​​ 中的哈希

common-lisp - 哪些标准 Common Lisp 宏/特殊形式建立了名为 nil 的隐式 block ?

php - cakePHP发送的电子邮件,如何在变量中设置管理员电子邮件地址?

mysql - 在数据库中存储值的问题(cakePHP)

java - 设置WebView大小

mysql - 使用不同的值作为列创建 MySQL View

mysql - CakePHP表单提交 "array"

php - CakePHP问题: How can i check whether there is an empty field or not after submitting form?

Android 等同于 KeyedArchiver,如 Ios