【发布时间】:2017-11-11 14:11:08
【问题描述】:
我想为 Cakephp formHelper 创建小部件。
我在 View/Widget 目录中创建了一个名为 ImageWidget.php 的小部件文件(我不知道这是否是正确的目录,但 phpstorm 会加载它,所以看起来不错)
<?php
namespace App\View\Widget;
use Cake\View\Form\ContextInterface;
use Cake\View\Widget\WidgetInterface;
class ImageWidget implements WidgetInterface
{
protected $_templates;
public function __construct($templates)
{
$this->_templates = $templates;
}
public function render(array $data, ContextInterface $context)
{
$data += [
'name' => '',
];
return $this->_templates->format('myTestTemp', [
'name' => $data['name'],
'attrs' => $this->_templates->formatAttributes($data, ['name'])
]);
}
public function secureFields(array $data)
{
return [$data['name']];
}
}
?>
但是我收到了这个错误:
找不到名为“myTestTemp”的模板。
我不知道我应该把我的模板放在哪里,或者这个模板是什么 (和 cakephp 普通模板一样吗?)
我的模板文件是这样的:
//in myTestTemp.ctp file
<p>some html for test</p>
我试图将文件放在这些目录中,但它不起作用
- src/模板
- src/查看
- src/视图/小部件
谢谢
【问题讨论】:
-
您的问题缺少问题的根源,即您定义使用名为
myTestTemp的(字符串)模板的部分。 ps,无论何时收到错误,请始终发布完成错误,即包括完整堆栈跟踪(理想情况下从日志中以正确可读的方式复制),即使问题对于熟悉 CakePHP 的人来说可能很明显! -
对不起,我在复制和粘贴过程中错过了它。感谢您的注意。
标签: cakephp cakephp-3.0