Home > Article > Backend Development > Smarty environment configuration and usage introductory tutorial php smarty Yu Guoli smarty assign
The examples in this article describe the configuration and use of Smarty environment. Share it with everyone for your reference, the details are as follows:
Download Smarty (here, we take Smarty-2.6.26 as an example). Unzip the downloaded file (the directory structure is quite complex). Next, I will show you an installation example. After reading it, you should be able to draw inferences.
(1) Create a new directory learn/ in the root directory, and then create a directory smarty/ in learn/. Copy the libs/ of the directory you just decompressed to smarty/, and then create a new templates directory in smarty/, and create cache/, templates/, templates_c/, and config/ in templates.
(2) Create a new template file: index.tpl. Place this file in the learn/smarty/templates/templates directory. The code is as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 <html> <head> <metahttp-equiv="Content-Type" c><pre class="brush:php;toolbar:false"><?php require 'smarty/libs/Smarty.class.php'; $smarty = new Smarty;//设置各个目录的路径,这里是安装的重点 $smarty->template_dir ="smarty/templates/templates"; $smarty->compile_dir ="smarty/templates/templates_c"; $smarty->config_dir = "smarty/templates/config"; $smarty->cache_dir ="smarty/templates/cache"; //smarty模板有高速缓存的功能,如果这里是true的话即打开caching,但是会造成网页不立即更新的问题,当然也可以通过其他的办法解决 $smarty->caching = false; $smarty->left_delimiter = "{#"; //重新定义边界,因为默认边界“{}“符,在html页面中嵌入js脚本文件编写代码段时使用的就是”{}“符,自定义边界符还可以是<{ }>, {/ /} 等 $smarty->right_delimiter = "#}"; $hello = "Hello World!";//赋值 $smarty->assign("hello",$hello);//引用模板文件 $smarty->display('index.tpl');?>
(3) Execute index.php to see Hello World !
Readers who are interested in more PHP-related content can check out the special topics of this site: "Basic Tutorial for Getting Started with Smarty Templates", "Summary of PHP Template Technology", "Complete of PHP Array (Array) Operation Skills", "PHP Operation Database Based on PDO" Summary of Skills", "Summary of PHP Operations and Operator Usage", "Summary of PHP Network Programming Skills", "Introduction Tutorial on PHP Basic Syntax", "Introduction Tutorial on PHP Object-Oriented Programming", "Summary of PHP String Usage" , "Introduction to php+mysql database operation tutorial" and "Summary of common php database operation skills"
I hope this article will be helpful to everyone's PHP program design based on smarty templates.
The above introduces the introductory tutorial for configuring and using the Smarty environment, including smarty content. I hope it will be helpful to friends who are interested in PHP tutorials.