php Smarty 模板简单实例

本文介绍了一个简单的PHP项目,使用Smarty模板引擎进行页面渲染的过程。包括项目文件目录结构、Smarty类库配置及模板文件的使用方法。

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

一、建立一个php Project,文件目录如下图

      

二、文件说明

       1、libs:是Smarty类库 

       2、tpl/cache_dir:存放缓存模板

       3、tpl/compile_dir:存放编译后模板文件

       4、tpl/config_dir:存放特殊配置文件

       5、tpl/template_dir:存放模板文件

       6、smarty.php文件里 new 出了一个 Smarty类对象,并设定各对象的属性值,如下代码

            <?php

                require 'libs/Smarty.class.php';//加载Smarty.class.php文件                

                define('SITE_ROOT','./tpl/');//定义一个常量

                $tpl = new Smarty();

                $tpl->template_dir = SITE_ROOT . 'template_dir';//存模板文件    

                $tpl->compile_dir = SITE_ROOT . 'compile_dir';//存编译过的模板文件

                $tpl->config_dir = SITE_ROOT . 'config_dir';//存特殊配置文件

                $tpl->cache_dir = SITE_ROOT . 'cache_dir';//存Smarty缓存文件    

                $tpl->caching = 1;//启用缓存

                $tpl->cache_lifetime = 60*60*24;//缓存时间1天    

                $tpl->left_delimiter = '<{';//左结束符

                $tpl->right_delimiter = '}>';//右结束符

        7、index.php文件 首页 代码如下

             <?php            

                 require 'smarty.php';

                $tpl->assign('title','title测试');

                $tpl->assign('content','content测试');

                $tpl->display('template.html');

        8、tpl/template_dir/template.html 这是一个模板文件 代码如下

             <html>                

                <head>                

                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

                        <title>

                            <{$title}>

                        </title>

                </head>

                <body>

                        <{$content}>

                </body>

             </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值