In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces PHP based on Yii framework how to use smarty template, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
The first method
It is a bit troublesome to generate views according to the method of YII system, and it is easier to use smarty. Tried to add the smarty template.
Date_default_timezone_set ("PRC"); class PlaceController extends CController {protected $_ smarty;function _ construct () {parent::__construct ('place'); / / requires a parameter to call the constructor of the parent class, which is the controller ID$path = Yii::getPathOfAlias (' application'); / / gets the absolute path to the protected folder include (dirname ($path). DIRECTORY_SEPARATOR.'Smarty'.DIRECTORY_SEPARATOR.'Smarty.class.php') / / smarty path $this- > _ smarty = new Smarty (); $this- > _ smarty- > template_dir = dirname ($path) .DIRECTORY_SEPARATOR.'template'.DIRECTORY_SEPARATOR;// template path}
One of the main problems is the problem of automatically loading class execution conflicts.
YII registers an auto-loading class spl_autoload_register (array ('YiiBase','autoload')), SMARTY also registers an auto-load class, spl_autoload_register (' smartyAutoload'), YII registers first, so that when a class name is encountered, the first execution is YII's custom auto-load class function. For each class name in SMARTY, it is also the function of calling YII's auto-load class first. However, if the condition of YII auto-loading is not met, the function of SMARTY's auto-loading class will be executed. however, when the class name of SMARTY automatically loads the class, it does conform to the logical statement of YII auto-loaded class. as a result, the class that YII uses the include statement will definitely not be found.
The solution is to jump out of the autoload function defined in YII when SMARTY's class loads automatically, so that the SMARTY's load function will be executed.
The specific implementation is to modify the autoload function in the YIIBase class and add the following code
Public static function autoload ($className) {/ / use include so that the error PHP file may appearif (preg_match ('/ smarty/i', $className)) {/ / as long as the class name contains smarty, it is returned regardless of case, so it jumps out of the YII autoload class to execute SMARTY's autoload class function return. } YII automatically loads class code}
This allows you to use smarty templates in each Action.
Public function actionIndex () {$this- > _ smarty- > assign ('test',' test'); $this- > _ smarty- > display ('create.html');}
The second method:
Put the smarty template plug-in in the extensions folder under protected, and create the CSmarty class file, as follows
Then create folders such as template_c,cache that are required for samrty.
Next comes the configuration section.
Open protected/config/main.php and add to the components array
'smarty'= > array (' class'= > 'application.extensions.CSmarty',)
Finally, you can try smarty by directly using Yii::app ()-> smarty in action. If it is troublesome to use Yii::app ()-> smarty in action every time, you can add it to Controller under components.
Protected $smarty ='; protected function init () {$this- > smarty = Yii::app ()-> smarty;}
Then you can use smarty directly with $this- > smarty in action.
Thank you for reading this article carefully. I hope the article "how to use smarty templates in PHP based on Yii framework" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.