joeytrapp / partials
该软件包最新版本(dev-master)没有可用的许可信息。
CakePHP 分部分辅助工具
dev-master
2013-10-17 20:21 UTC
Requires
- php: >=5.2.0
- composer/installers: *
This package is not auto-updated.
Last update: 2024-09-24 05:40:26 UTC
README
安装
将仓库下载到插件目录中
cd Plugin
git clone git://github.com:joeytrapp/partials.git Partials
在应用的 bootstrap.php
中添加 CakePlugin::load()
。
echo "CakePlugin::load("Partials");" >> Config/bootstrap.php
加载辅助工具,使其在视图中可用
public $helpers = array("Html", "Form", "Partials.Partial");
方法
PartialHelper::render(string $name, array $data, array $options)
在 View::viewPath 相对路径下渲染元素,而不是 View/Elements。假设元素文件以下划线开头。
<?php echo $this->Partial->render("form"); ?>
将在 View::viewPath 中寻找 _form.ctp(对于 /posts/add,将是 View/Posts/_form.ctp)。
$data 和 $options 参数将传递给 View::element()
方法。还有一个额外的选项 "collection"。如果 "collection" 是一个数组,那么 View::element()
将对 "collection" 数组中的每个迭代进行调用。当前迭代的值将合并到数据数组中,以元素名称(不带下划线)作为键。
<?php echo $this->Partial->render("post", array(), array("collection" => $posts)); ?>
这将针对 $posts 中的每个值渲染 _post.ctp
元素,并将 $posts 中的值设置为数据数组中的 "post" 键。