如何创建在CakePHP3中使用TemplaterTrait的新助手?

时间:2015-02-13 10:53:56

标签: cakephp cakephp-3.0

我正在尝试创建一个新的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的非对象。

FormHelperHtmlHelper如何使用模板而不会产生同样的错误?

1 个答案:

答案 0 :(得分:2)

您实际上并未使用该特征,您只需将其导入当前命名空间即可。要实际使用它,必须使用use语句在类定义中指定它。

class PortletHelper extends Helper
{
    use StringTemplateTrait;

    // ...
}

另请参阅 http://php.net/manual/en/language.oop5.traits.php