【问题标题】:Using CakePHP helpers with TwigView plug-in not working使用带有 TwigView 插件的 CakePHP 助手不起作用
【发布时间】:2014-02-04 10:33:27
【问题描述】:

我已经在 CakePHP 应用程序中安装了 TwigView,我正在尝试在名为 default.twig.tpl 的默认模板中加载元素 header.twig.tplfooter.twig.tpl

这个很好用:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Hello world!</title>

</head>
<body>
{% element 'header.twig' %}

    {{ test_var_from_controller }} <!-- this works and echoes "Hello world!" as expected -->

{% element 'footer.twig' %}
</body>
</html>

当我尝试使用CakePHP 默认Helpers 时,问题就开始了,它们被简单地忽略了。

我不知道为什么,如果我尝试:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Hello world!</title>
    {{ html.css('styes.min') }}
</head>

忽略该方法而不会抛出错误,并且页面仍然像从未调用过一样工作。

我已经关注了所有 TwigView explanation,但它仍然无法正常工作,我错过了什么?

我也在尝试使用 Twig 函数,但由于某种原因它们似乎没有加载:

{{ 'FOO'|low }}

抛出错误:

Fatal error: Call to undefined function low() in .../app/Plugin/TwigView/tmp/views/2d/4b/65accc...92.php on line 45

【问题讨论】:

  • 正如我所说我没有得到错误,它们被忽略了。
  • 我还发现twig找不到dump方法{{ dump(some_var) }},并抛出错误:The function "dump" does not exist in "Layouts/default.twig.tpl" at line 22
  • 我也有加载树枝到 cakephp 的问题,你能告诉我如何加载插件吗?

标签: twig helper template-engine cakephp-2.4


【解决方案1】:

我找到了问题,我需要先在 AppController 中加载 Helpers

class AppController extends Controller {
    public $viewClass = 'TwigView.Twig';
    public $ext = '.twig.tpl';
    public $helpers = array('Html', 'Form');
}

【讨论】:

    【解决方案2】:

    在 CakePHP 3 中,如果你使用 WyriHaximus 插件,你可以把它放在 src/View/AppView.php 中

    namespace App\View;
    use WyriHaximus\TwigView\View\TwigView;
    
    /**
     * Application View
     */
    class AppView extends TwigView
    {
      /**
       * Initialization hook method.
       * Use this method to add common initialization code like loading helpers.
       * e.g. `$this->loadHelper('Html');`
       * @return void
       */
      public function initialize()
      {
        parent::initialize();
        $this->loadHelper('Html');
        $this->loadHelper('Flash');
        $this->loadHelper('Url');
        ...
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-08
      • 2012-08-09
      • 2015-05-31
      • 2013-07-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2018-12-08
      相关资源
      最近更新 更多