Smarty的安装

本文介绍了Smarty模板引擎的基本使用方法,包括如何安装、配置和使用Smarty进行动态页面展示,并解决了JS代码冲突的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

smarty是一个PHP模板引擎,用于区分美工与程序

可以在www.smarty.net官方网站下载,有smarty 2.X、smarty 3.X,smarty 3生成的编译后的文件很大,暂时使用Smarty 2

Smarty/libs包含了核心文件


test.php

<?php

require('./libs/Smarty.class.php');//包含smarty类文件

$smarty = new Smarty;//创建smarty对象

$smarty->template_dir = './templates/';//指定模板文件所在位置
$smarty->compile_dir = './templates_c/';//指定编译后的文件所在位置
$smarty->config_dir = './configs/';//指定配置文件
$smarty->cache_dir = './cache/';//缓存文件

$smarty->assign('name','ChuangRain');//分配变量

$smarty->display('index.html');//显示templates/index.html
index.html
{* Smarty *}//注释

hello,{$name}!//输出 hello,ChuangRain

如果是现在这种{}标签,js会出问题,因为在js中会用到{},在smarty编译时会将其当作是smarty要编译的东西,会出错。

例index.html

{* Smarty *}

hello,{$name}!

<script>

function test(){
	alert('11111111111111');
}

test();

</script>
运行时会出现下面的错误信息

Fatal error: Smarty error: [in index.html line 7]: syntax error: unrecognized tag: alert('11111111111111'); (Smarty_Compiler.class.php, line 446) in D:\AppServ\www\Smarty\libs\Smarty.class.php on line 1093

可以用literal来忽略{},

{literal}
    <script>
        function test(){
              ...
        }
    </script>
{/literal}
<!-- 在{/literal}和{/literal}之间的内容会被当作文本来处理,忽略其内容 -->

但是一般都是修改smarty的标签,来解决这个问题

$smarty->left_delimiter = "<{";//左标签 <{
$smarty->right_delimiter = "}>";//右标签 }>

在模板中使用变量就可以直接用 <{$name}>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值