Home > Backend Development > PHP Tutorial > Specific guide to using PHP template Smarty_PHP tutorial

Specific guide to using PHP template Smarty_PHP tutorial

WBOY
Release: 2016-07-15 13:30:44
Original
1140 people have browsed it

Usually our solution is to have the artist design the page and hand it over to the programmer for development, and then hand it over to the artist to improve the page, repeating it several times. If the programmer encounters a problem If you are not familiar with HTML, it will be a painful task for both parties and the efficiency will be lower. At this time, it is very important to have template support.

We know that the PHP language, as a member of the open source community, provides various template engines, such as FastTemplate, Smarty, SimpleTemplate, etc., and Smarty is the most commonly used PHP template engine today. I will share it with you today How to install and use PHP template Smarty in PHP development can also be regarded as an introduction to Smarty.

Preparation

1. Select the directory where Smarty is installed

If you have server permissions, consider security. Install Smarty outside the WEB program's documentation directory, and then include the address of the Smarty installation directory in the include_path option in the PHP.INI file.

If you have virtual host permissions or several projects, you can install Smarty in their respective project directories. You can also use the Smarty template engine in the require Smarty class file. Of course, for security reasons, you can prohibit access to related directories through apache.

In addition, the two PHP template Smarty installation methods are different in terms of portability. The first method needs to ensure that each server has the same Smarty configuration, while the second method has no impact on the configuration of each server. You can choose the installation method of Smarty according to your own needs.

2. Download Smarty, I chose Smarty-2.6.25

PHP template Smarty installation steps

1. Unzip the downloaded Smarty-2.6.25 compressed package

2. Copy the libs folder to the WEB program directory. My directory is testsmarty

You can refer to here for the installation method under Linux.

After installing the Smarty template, we started to use Smarty simply.

Using PHP template Smarty

1. Create related directories

Due to the process of using Smarty, Smarty will generate For compiled template files, other configuration files, and cache files, we need to create relevant directories. I created another tpls directory in the testsmarty directory, and created templates, templates_c, configs, and cache directories in the tpls directory. Why do we need to create these directories? Open the Smarty.class.php file, and we can see that the Smarty class defines some member attributes.

$template_dir: Set the directory address where all template files need to be placed. By default, the directory is: "./templates", that is, the template directory is found in the same directory as the PHP execution program.

$compile_dir: Set the storage directory address of all template files compiled by Smarty. The default directory is: "./templates_c", that is, the compilation directory is found in the same directory as the PHP execution program. If you create this directory on a Linux server, you also need to modify the permissions of this directory so that it has write permissions.

$config_dir: Set the directory used to store special configuration files for templates. The default directory is: "./configs", that is, the configuration directory is found in the same directory as the PHP execution program.

$cache_dir: When the caching feature is enabled, all templates cached by PHP Template Smarty are placed in the directory specified by this attribute. The default directory is: "./cache", that is, the cache directory is found in the same directory as the PHP execution program. You can also use your own custom cache handler to control cached files, which will ignore this setting. Similarly, if you create this directory on a Linux server, you also need to modify the permissions of this directory so that it has write permissions.

For system security and portability considerations, it is recommended not to create these directories in the same directory as the PHP executable program. You can create them outside the PHP executable program directory. If they have been created in the same directory as the PHP executable program , you can use Apache to restrict directory access.

2. Create related configuration files

We need to create a configuration file to override the default member attributes of the PHP template Smarty class, and name it main.php, save it In the smarty directory, which script needs to use Smarty in the future, we only need to include main.php.

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?  </span></span></li><li><span>include("./smarty/libs/Smarty.class.php");  </span></li><li class="alt"><span>define('SMARTY_ROOT', './smarty/tpls');  </span></li><li><span>$</span><span class="attribute">tpl</span><span> = </span><span class="attribute-value">new</span><span> Smarty();  </span></li><li class="alt"><span>$tpl-</span><span class="tag">></span><span class="attribute">template_dir</span><span> = </span><span class="attribute-value">SMARTY_ROOT</span><span>."/templates/";  </span></span></li>
<li>
<span>$tpl-</span><span class="tag">></span><span class="attribute">compile_dir</span><span> = </span><span class="attribute-value">SMARTY_ROOT</span><span>."/templates_c/";  </span>
</li>
<li class="alt">
<span>$tpl-</span><span class="tag">></span><span class="attribute">config_dir</span><span> = </span><span class="attribute-value">SMARTY_ROOT</span><span>."/configs/";  </span>
</li>
<li>
<span>$tpl-</span><span class="tag">></span><span class="attribute">cache_dir</span><span> = </span><span class="attribute-value">SMARTY_ROOT</span><span>."/cache/";  </span>
</li>
<li class="alt">
<span>$tpl-</span><span class="tag">></span><span class="attribute">caching</span><span>=</span><span class="attribute-value">1</span><span>;  </span>
</li>
<li>
<span>$tpl-</span><span class="tag">></span><span class="attribute">cache_lifetime</span><span>=</span><span class="attribute-value">60</span><span>*60*24;  </span>
</li>
<li class="alt">
<span>$tpl-</span><span class="tag">></span><span class="attribute">left_delimiter</span><span> = </span><span class="attribute-value">'< {'</span><span>;  </span></li><li><span>$tpl-</span><span class="tag">></span><span class="attribute">right_delimiter</span><span> = </span><span class="attribute-value">'}>'</span><span>;  </span>
</li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

Note:

Lines 1-8: Mainly define a smarty object, and set the template file, compiled file, cache file, and configuration file at the same time Storage directory, overwriting the default value in Smarty.class.php.

Lines 9-10: Set to enable caching and set the cache validity time to 1 day.
Knowledge point: $caching is used to set whether to enable the cache function. Default value is set to 0 or invalid. You can also have multiple caches for the same template, and enable caching when the value is 1 or 2. 1 tells Smarty to use the current $cache_lifetime variable to determine whether the cache has expired. 2 Tell PHP template Smarty to use the cache_lifetime value when generating the cache. It is recommended to turn off the cache during project development and set the value to 0

Lines 11-12: Set the left and right terminators of the smarty language. We know that curly brackets are the default delimiters of smarty, but when used with javascript , css, etc. may cause conflicts, so here we set it to <{and}>.

3、建立一个模板文件

一般情况下在美工页面设计完毕后,双方的交集点是模版文件,双方约定后,程序员不需要花太大的精力在前台,这就是使用Smarty模板引擎进行开发的好处。

我们首先建立一个简单的模版文件,名为leapsoul.tpl,你可在html文件中加入smarty变量后将文件另存为tpl类型的文件。

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> </span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li>
<li>
<span class="tag"><</span><span> </span><span class="tag-name">head</span><span class="tag">></span><span> </span>
</li>
<li class="alt">
<span class="tag"><</span><span> </span><span class="tag-name">meta</span><span> </span><span class="attribute">http-equiv</span><span>=</span><span class="attribute-value">"Content-type"</span><span> <br /></span><span class="attribute">content</span><span>=</span><span class="attribute-value">"text/html; charset=gb2312"</span><span class="tag">></span><span> </span>
</li>
<li>
<span class="tag"><</span><span> </span><span class="tag-name">title</span><span class="tag">></span><span>   </span>
</li>
<li class="alt">
<span class="tag"><</span><span> { $title }</span><span class="tag">></span><span> </span>
</li>
<li>
<span class="tag"><</span><span> /title</span><span class="tag">></span><span>   </span>
</li>
<li class="alt">
<span class="tag"><</span><span> /head</span><span class="tag">></span><span>   </span>
</li>
<li>
<span> </span><span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span>   </span>
</li>
<li class="alt">
<span class="tag"><</span><span> { $content }</span><span class="tag">></span><span>   </span>
</li>
<li>
<span class="tag"><</span><span> /body</span><span class="tag">></span><span>   </span>
</li>
<li class="alt">
<span class="tag"><</span><span> /html</span><span class="tag">></span><span> </span>
</li>
</ol>
Copy after login

注释:在这个tpl文件中设定了title和content两个PHP模板Smarty变量,文件保存为leapsoul.tpl,同时将其保存在testsmartytplstemplates模板文件目录下。

4、建立应用程序文件

模版文件类似于一个表现层,在建立完模板文件后,我们需要一个应用程序去驱动表现层,应用程序文件定义为smarty.php。

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?  </span></span></li><li><span>include("smarty/main.php");  </span></li><li class="alt"><span>$tpl-</span><span class="tag">></span><span>assign("title", "leapsoul.cn为你<br>展示smarty模板技术");  </span></span></li>
<li>
<span>$tpl-</span><span class="tag">></span><span>assign("content", "leapsoul.cn<br>通过详细的安装使用步骤为你展示smarty模板技术");  </span>
</li>
<li class="alt">
<span>$tpl-</span><span class="tag">></span><span>display("leapsoul.tpl");  </span>
</li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

注释:

在这段代码中我们主要用到smarty中的两个函数assign和display,assign你可以理解为为变量赋值,display主要是用来将网页输出。更多smarty函数今后会详细介绍。

其他说明

由于我们开启了缓存功能,有兴趣的朋友可以打开cache和templates_c,cache目录存放了这个模板的缓存文件,文件开头部分有缓存信息,如文件的生成时间和过期时间等,其他的和一般的HTML文件没有多大的区别,而templates_c存放了模板经过编译后的PHP执行文件。

至此一个简单入门的PHP模板Smarty应用实例就算介绍完成了。 


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446267.htmlTechArticle通常我们的解决方法是由美工设计页面后交付程序设计者进行开发,再交付美工对页面进行改善,来回重复好几回,如果遇到程序设计者对...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template