php教程 php手册 Yii2-smarty的一些小坑

Yii2-smarty的一些小坑

Jun 06, 2016 pm 08:14 PM

在写本文前我不得不说一句,其实我是不想用smarty的,我想尝试一下twig,但是phpstorm的Twig插件真要命,卡成翔,所以我只能用smarty。为什么不用prado了呢?官方说不支持了,我晶啊 在使用smarty的时候官方的代码和例子看上去很美,不过要注意几点 1、用yii

在写本文前我不得不说一句,其实我是不想用smarty的,我想尝试一下twig,但是phpstorm的Twig插件真要命,卡成翔,所以我只能用smarty。为什么不用prado了呢?官方说不支持了,我晶啊

在使用smarty的时候官方的代码和例子看上去很美,不过要注意几点

1、用yii2-smarty,还是必须得用layout,如果你不支持layout文件,默认就是/layouts/main.php,天啊,为什么是PHP?而且在这里面也还真的能用PHP代码。整个都崩溃了

2、你可以指定layout文件,比如:main.tpl,OK你必须得象PHP文件一样,得写{$this->head()},{$this->startBody()}{$this->endPage()}等,否则 ClientScript功能就无法使用

3、如果你指定layout=false,那么,就不支持ClientScript了。因为你incude file='xxx.tpl',在每一个独立的文件里都必须要象2中一个个的this->head(),this->endPage全写上

4、再来一个bug:{registerJsFile url=''},这个函数有BUG

原来是:

    public function functionRegisterJsFile($params, $template)  
    {  
        if (!isset($params['url'])) {  
            trigger_error("registerJsFile: missing 'url' parameter");  
        }  
        $url = ArrayHelper::remove($params, 'url');  
        $key = ArrayHelper::remove($params, 'key', null);  
        $depends = ArrayHelper::remove($params, 'depends', null);  
        if (isset($params['position']))  
            $params['position'] = $this->getViewConstVal($params['position'], View::POS_END);  
        Yii::$app->getView()->registerJsFile($url, $depends, $params, $key);  
    }  
로그인 후 복사

改成为:

    /** 
     * Smarty function plugin 
     * Usage is the following: 
     * 
     * {registerJsFile url='http://maps.google.com/maps/api/js?sensor=false' position='POS_END'} 
     * 
     * Supported attributes: url, key, depends, position and valid HTML attributes for the script tag. 
     * Refer to Yii documentation for details. 
     * The position attribute is passed as text without the class prefix. 
     * Default is 'POS_END'. 
     * 
     * @param $params 
     * @param \Smarty_Internal_Template $template 
     * @return string 
     * @note Even though this method is public it should not be called directly. 
     */  
    public function functionRegisterJsFile($params, $template)  
    {  
        if (!isset($params['url'])) {  
            trigger_error("registerJsFile: missing 'url' parameter");  
        }  
        $url = ArrayHelper::remove($params, 'url');  
        $key = ArrayHelper::remove($params, 'key', null);  
        $params['depends'] = ArrayHelper::remove($params, 'depends', null);  
        if (isset($params['position']))  
            $params['position'] = $this->getViewConstVal($params['position'], View::POS_END);  
        Yii::$app->getView()->registerJsFile($url, $params, $key);  
    }  
로그인 후 복사

其实就是$params['depends']这个参数。registerJsFile只能接受3个参数,但事实上用了4个参数,所以调整一下即可

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)