2014-01-15 36 views
1

我已經在我的CakePHP應用程序中安裝了TwigView,我試圖在名爲default.twig.tpl的默認模板中加載元素header.twig.tplfooter.twig.tpl使用CakePHP幫手,TwigView插件無法正常工作

它正常工作與此:

<!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但它仍然無法正常工作,我錯過了什麼?

我也想用嫩枝功能,他們似乎不加載某些原因:

{{ 'FOO'|low }} 

拋出錯誤:

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

show error please? – Anubhav

+0

正如我所說我沒有得到錯誤,他們簡直被忽略。 – vitto

+0

我還發現''{dump(method)'{{dump(some_var)}}'沒有被twig發現,並且拋出一個錯誤:''Layouts/default.twig.tpl中不存在函數「dump」在線22' – vitto

回答

1

我發現這個問題,我需要加載助手AppController的第一

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

內部在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'); 
    ... 
    } 
}