Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在CakePHP3中创建使用TemplaterTrait的新帮助器?_Cakephp_Cakephp 3.0 - Fatal编程技术网

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

如何在CakePHP3中创建使用TemplaterTrait的新帮助器?,cakephp,cakephp-3.0,Cakephp,Cakephp 3.0,我正在尝试创建一个新的助手,我打算使用模板 我的代码如下 <?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

我正在尝试创建一个新的助手,我打算使用模板

我的代码如下

<?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']);
    }
}

您实际上并没有使用trait,您所做的只是将其导入当前名称空间。要实际使用它,必须使用
use
语句在类定义中指定它

class PortletHelper extends Helper
{
    use StringTemplateTrait;

    // ...
}
另请参见