Home > Backend Development > PHP Tutorial > 如何在yii2-twig模板中使用use

如何在yii2-twig模板中使用use

WBOY
Release: 2016-06-06 20:30:47
Original
1559 people have browsed it

我自定义了一个helpers类:app/helpers/SiteHelper

<code>namespace app\helpers;

use Yii;
use app\models\Config;

class SiteHelper
{

    /**
     * 获取title
     * @param string $title
     * @return string
     */
    public static function getTitle($title = '')
    {
        $sitename = Config::getConfigValue('site_name') ? : Yii::$app->name;
        return $title ? $title . ' - ' . $sitename : $sitename;
    }
}
</code>
Copy after login
Copy after login

在layout模板main.twig中定义:

<code>{{ use('/app/helpers/SiteHelper') }}
</code>
Copy after login
Copy after login

但是使用

<code>{{ SiteHelper.getTitle(this.title) }}
</code>
Copy after login
Copy after login

却没有作用,是不是我用错了?到底该如何使用use呢?

回复内容:

我自定义了一个helpers类:app/helpers/SiteHelper

<code>namespace app\helpers;

use Yii;
use app\models\Config;

class SiteHelper
{

    /**
     * 获取title
     * @param string $title
     * @return string
     */
    public static function getTitle($title = '')
    {
        $sitename = Config::getConfigValue('site_name') ? : Yii::$app->name;
        return $title ? $title . ' - ' . $sitename : $sitename;
    }
}
</code>
Copy after login
Copy after login

在layout模板main.twig中定义:

<code>{{ use('/app/helpers/SiteHelper') }}
</code>
Copy after login
Copy after login

但是使用

<code>{{ SiteHelper.getTitle(this.title) }}
</code>
Copy after login
Copy after login

却没有作用,是不是我用错了?到底该如何使用use呢?

亲 能私信下QQ么 我这几天正在用yii的twig 但是报错说twig未找到

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template