我正在尝试创建一个新的Helper,我打算使用templater。
我的代码如下。
<?php
/**
* KimSia
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright TBD
* @link TBD
* @since 0.1
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Metronic\View\Helper;
use Cake\Core\Configure;
use Cake\View\Helper;
use Cake\View\StringTemplateTrait;
use Cake\View\View;
/**
* Portlet helper library.
*
* Automatic generation of Portlet divs and HTML FORMs from given data.
*
* @property HtmlHelper $Html
* @property FormHelper $Form
* @link TBD
*/
class PortletHelper extends Helper
{
/**
* Other helpers used by PortletHelper
*
* @var array
*/
public $helpers = ['Url', 'Html', 'Form'];
/**
* Default config for the helper.
*
* @var array
*/
protected $_defaultConfig = [
'templates' => [
'portletStart' => '<!-- BEGIN FORM PORTLET--><div class="portlet box {{color}}">',
'portletEnd' => '</div><!-- END FORM PORTLET-->',
]
];
/**
* Construct the widgets and binds the default context providers
*
* @param \Cake\View\View $View The View this helper is being attached to.
* @param array $config Configuration settings for the helper.
*/
public function __construct(View $View, array $config = [])
{
parent::__construct($View, $config);
}
/**
* Returns an Portlet DIV element.
*
* ### Options:
*
* - `color` Color for the Portlet
*
* @param array $options An array of html attributes and options.
* @return string An formatted opening FORM tag.
* @link TBD
*/
public function create(array $options = [])
{
$defaultOptions = [
'color' => 'yellow',
];
$options = array_merge($options, $defaultOptions);
$templater = $this->templater();
return $templater->format('portletStart', [
'color' => $options['color']
]);
}
/**
* Closes a Portlet DIV, cleans up values set by PortletHelper::create(), and writes hidden
* input fields where appropriate.
*
* @return string A closing DIV tag.
* @link TBD
*/
public function end()
{
$out = '';
$templater = $this->templater();
$out .= $templater->format('portletEnd', []);
$templater->pop();
return $out;
}
/**
* Restores the default values built into FormHelper.
*
* This method will not reset any templates set in custom widgets.
*
* @return void
*/
public function resetTemplates()
{
$this->templates($this->_defaultConfig['templates']);
}
}
我在$templater->format
收到错误,说我有$templater
的非对象。
FormHelper
和HtmlHelper
如何使用模板而不会产生同样的错误?
答案 0 :(得分:2)
您实际上并未使用该特征,您只需将其导入当前命名空间即可。要实际使用它,必须使用use
语句在类定义中指定它。
class PortletHelper extends Helper
{
use StringTemplateTrait;
// ...
}