在yii2中常用的twig中使用php常量

时间:2018-04-13 18:03:31

标签: php yii2 twig twig-extension

yii2附带的默认站点有一个about页面,其中包含以下php代码。

<code><?= __FILE__ ?></code>

是否可以使用twig语法执行相同的操作?

我尝试了{{ __FILE__ }}{{ constant(__FILE__) }},但这些都没有奏效,后者产生的错误为constant(): Couldn't find constant

1 个答案:

答案 0 :(得分:-1)

尝试将参数从控制器传递到视图。

示例代码:

//Your view 
$loader = new Twig_Loader_Array(array(
    'index' => 'Hello {{ __FILE__ }}!',
));

//Your controller (see the second parameter in 'render')
$twig = new Twig_Environment($loader);    
echo $twig->render('index', array('__FILE__' =>  __FILE__ ));