在写本文前我不得不说一句,其实我是不想用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个参数,所以调整一下即可
原文地址:Yii2-smarty的一些小坑, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.