PHP使用smarty模板引擎

Smarty是一个使用PHP写出来的模板引擎,目的是分离php代码和html代码。是一个编译型模板引擎。

安装

(1)下载Smarty,

(2)在web服务器文档根目录新建一个smarty/文件夹,

(3)把解压Smarty后lib文件夹里的文件拷到smarty/下,

(4)在smarty/下新建4个文件夹template/自已的模板文件,templates_c/编译时生成的文件,cache/缓存的文件,configs/配置文件。

(5)编码指定(4)中的4个文件夹路径。在文档根目录下新建文件smart.php

<?php
require 'smarty/libs/Smarty.class.php';
$smarty = new Smarty();//设置各个目录的路径,这里是安装的重点
$smarty->template_dir ="smarty/libs/templates/templates";
$smarty->compile_dir ="smarty/libs/templates/templates_c";
$smarty->config_dir = "smarty/libs/templates/config";
$smarty->cache_dir ="smarty/libs/templates/cache";
?>

使用

(1)在文档根目录/smarty/template/下新建模板文件index.tpl

<html>
<head>
<title>smarty demo</title>
</head>
<body>
It's as easy as {$test}.
</body>
</html>

(2)在文档根目录/新建php文件index.php
<?php
require_once("smarty.php");
$smarty->assign('test', 123);
$smarty->display('index.tpl');
?>
(3)通过浏览器来访问index.php,http://localhost/index.php就会显示:

It's sa easy as 123.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值