1. What is smarty?
smarty is a method that uses php The template written by php template engine provides the separation of logic and external content. Simply put, the purpose is to use phpSeparation of programmers and artists,The programmer used to change the logical content of the program will not affect the artist's page design, and the artist's re-modification of the page will not It affects the program logic of the program, which is particularly important in multi-person cooperation projects.
2. smartyAdvantages:
1. Speed: Programs written using smarty can achieve maximum The speed improvement is compared to other template engine technologies.
2. Compiled type: Programs written using smarty must be compiled into a non-template technology php file, this file uses a mixture of php and html. The next time you access the template, web requests are converted directly into this file without template recompiling (when the source program is not changed)
3.Caching technology: smarty is a caching technology that can convert the htmlCache the file into a static html page, when setting smarty's cache When the attribute is true, the cachetime set in smarty During this period, the user's web request is directly converted into this static html file, which is equivalent to calling a static html file. 4.
Plug-in technology:smartyYou can customize plug-ins. Plug-ins are actually some custom functions. 5.
You can useif/elseif/else/endif in templates. Using judgment statements in template files can very conveniently reformat the template. 3. Places where
smartyis not suitable for use: 1.
Content that needs to be updated in real time. For example, like stock display, it needs to update data frequently. Usingsmarty in this type of program will slow down the template processing speed. 2.
Small project. For small projects where the artist and programmer are both simple, usingsmarty will lose the advantage of rapid development of php.
4. smarty installation and configuration:After downloading and decompressing the smarty installation package, place the libs folder in the root directory of the website, and then create several folders
templates to store template files
templates_c stores compiled files
configs Store configuration files
cache Store the cache file
Create the initialization file smarty.init.php
<span><?<span>php </span><span>include</span> "./libs/Smarty.class.php";<span>//</span><span>包含Smarty类库所在的文件</span> <span>$smarty</span>=<span>new</span> Smarty();<span>//</span><span>创建一个Smarty类的对象$smarty</span> <span>$smarty</span>->template_dir="./templates/";<span>//</span><span>设置所有模板文件存放目录</span> <span>$smarty</span>->compile_dir="./templates_c/";<span>//</span><span>设置所有编译过的模板文件存放目录</span> <span>$smarty</span>->config_dir="./configs/";<span>//</span><span>设置模板中特殊配置文件存放的目录</span> <span>$smarty</span>->cache_dir="./cache/";<span>//</span><span>设置存放Smarty缓存文件的目录</span> <span>$smarty</span>->caching=1;<span>//</span><span>设置开启Smarty缓存模板功能</span> <span>$smarty</span>->cache_lifetime=60*60*24;<span>//</span><span>设置模板缓存有效时间为1天</span> <span>$smarty</span>->left_delimiter='<{';<span>//</span><span>设置模板语言中的左结束符</span> <span>$smarty</span>->right_delimiter='}>';<span>//</span><span>设置模板语言中的右结束符</span> ?></span>
1. Create a new template file index.tpl and the configuration file my.conf required in the project. The configuration file can also be added later
<span><span><</span><span>{config_load </span><span>file</span><span>="../configs/my.conf"</span><span>}</span><span>></span><span><!--</span><span> 加载配置文件 </span><span>--></span> <span><</span><span>html</span><span>></span> <span><</span><span>head</span><span>></span> <span><</span><span>meta </span><span>charset</span><span>="utf-8"</span><span>></span> <span><</span><span>title</span><span>><</span><span>{$title</span><span>}</span><span>></</span><span>title</span><span>></span> <span></</span><span>head</span><span>></span> <span><</span><span>body </span><span>bgcolor</span><span>="<{#bgColor#}>"</span><span>></span> <span><</span><span>{$content</span><span>}</span><span>></span> <span></</span><span>body</span><span>></span> <span></</span><span>html</span><span>></span></span>
<span><?<span>php </span><span>//</span><span>引入smarty.init.php</span> <span>include</span> 'smarty.init.php'<span>; </span><span>$smarty</span>->assign("title","我的第一个文件标题"<span>); </span><span>$smarty</span>->assign("content","我的第一个文件内容"<span>); </span><span>$smarty</span>->display("index.tpl"<span>); </span>?></span>
http://www.bkjia.com/PHPjc/1096613.html