smarty installation and examples

WBOY
Release: 2016-07-29 09:05:58
Original
899 people have browsed it

Environment:

smarty

1. Download the latest smarty package at http://www.smarty.net/download, select zips for window, and tar.gz for linux. Taking Windows as an example, download and unzip it, such as f:smarty.

2. Copy the lib directory in the unzipped smarty directory to test and rename it to smarty. In the test directory, create the tpls directory. In the tpls directory, create the templates, templates_c, configs, and cache directories. These directories are the template directory (required), the parsing directory (required), the configuration directory (optional), and the cache. Directory (optional),

smarty’s php code is at the same level as these four directories, and the html code is placed under templates.

The directory tree is as follows

smarty installation and examples

Code part:

1. Create main.php in utf-8 no BOM format under test/smarty, and configure some member attributes of smarty.

smarty installation and examples

 1 <?php 2 include("Smarty.class.php");
 3 define('SMARTY_ROOT', '../tpls');
 4 $tpl = new Smarty();
 5 $tpl->template_dir = SMARTY_ROOT."/templates/";//设置模板文件的存放目录
 6 $tpl->compile_dir = SMARTY_ROOT."/templates_c/";//设置编译文件的存放目录
 7 $tpl->config_dir = SMARTY_ROOT."/configs/";//设置配置文件的存放目录
 8 $tpl->cache_dir = SMARTY_ROOT."/cache/";//设置缓存文件的存放目录
 9 $tpl->caching=1;//开启缓存
10 $tpl->cache_lifetime=60*60*24;//有效时间为一天
11 $tpl->left_delimiter = '[';//smarty语言的左右结束符
12 $tpl->right_delimiter = ']';
13 ?>
Copy after login

smarty installation and examples

We know that braces are the default delimiter of smarty, but they may conflict when combined with javascript, css, etc., so here we set it to [and] .

2. Create a new html.tpl template file under test/tpls/templates, which is to add smarty variables to html. Changing the template is equivalent to the presentation layer. The code of

html.tpl is as follows:

smarty installation and examples

 1 
 2 
 3 <meta http-equiv="Content-type" c><span><img src="http://image.codes51.com/Article/image/20160121/20160121091057_4965.gif" alt="smarty installation and examples"></span><p>3.在test目录下创建smarty.php,该文件相当于驱动层,给上面表现层的变量赋好值,然后显示出来。</p><p>smarty.php的代码如下:</p><p></p><pre class="brush:php;toolbar:false">1 <?php 2     include("smarty/main.php");
3     $tpl->assign("title","迟到");
4     $tpl->assign("content","罚款500元!");
5     $tpl->display("tpls/templates/html.tpl");
6 ?>
Copy after login

4. Run smarty.php in the browser.

The above introduces smarty installation and examples, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!