smarty模版技术

本文介绍了Smarty模板引擎,它致力于解决PHP和HTML分离问题。阐述了其本质、下载解压步骤、优缺点,还说明了加载、实例化、使用及配置方法。同时介绍了模版变量(PHP分配、保留、自定义)、配置文件,以及内置函数(分支、循环结构)和外部函数的使用。

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

当HTML不可避免的要写入数据时 不让PHP便签显示在HTML代码中 从而让代码看上去更整洁
利用特定的占位符放到一个文档(HTML)中占据某个位置然后利用相应的数据替代占位符
***本质***本质就是在HTML中设置相对简洁的标签 然后利用PHP实现内容的替换.
下载 官网
解压 解压之后我们只需要其中libs文件夹下的内容 全选 复制到根目录 可以新建一个vendors文件夹把内容黏贴进去
plugins文件夹 开发人员插件
syspluanins 系统插件文件夹
autoload.php smarty内部增加文件
smarty.class.php smarty核心入口文件
smartyBL.class.php smarty兼容入口文件
优缺点
效率高 编译型 缓存 插件 灵活(内置函数)
但是实时效率低 所以一些实时性很强那个的项目例如股票等 不可以使用smarty模板
编译型 不推荐小项目使用
smarty是一种致力于解决PHP和HTM L分离的模板引擎

<html>
    <header></header>
    <body>
        <!--smarty定义占位符-->
        {$hello}
    </body>
</html>
<?php
#加载smarty.class.php核心入口文件
include_once "./vendors/smarty.class.php";
#实例化对象
$s = new smarty();
#定义数据
$hello = "helloworld smarty";
#assign方法 
$s->assign('hello',$hello);
//第一个参数是占位符 不带$ 第二个参数是具体要传入的数据
//数据交给smaety系统
$s->display('./template/01smarty.html');
?>

加载 smarty.lass.php文件
实例化对象 后面会使用到对象的方法和属性
模版文件中定义占位符 {$变量名}
assign 第一个参数’变量名 第二个参数 具体数据
display 加载模板并显示出来
使用
smarty之后系统会自动在根目录生成一个template_c文件夹 用来存放编译后的混合文件
配置
samety支持在PHP文件中对smarty进行配置 利用对象的属性

<?php
#加载smarty.class.php核心入口文件
include_once "./vendors/smarty.class.php";
#实例化对象
$s = new smarty();
#配置模版默认文件夹
$s->template_dir='./template/';
#定义数据
$hello = "helloworld smarty";
#assign方法 
$s->assign('hello',$hello);
//第一个参数是占位符 不带$ 第二个参数是具体要传入的数据
//数据交给smaety系统
//$s->display('./template/01smarty.html');
$s->display('01smarty.html');
?>

模版变量
smarty的本质就是在PHP文件中用值替换掉模版中用变量表示的占位符
smarty支持以下几种变量
PHP分配变量
最常用的 当时标量数据时直接分配 直接使用
数据数据时 传入数据 靠下标访问
对象数据时 传入对像 靠对象->访问

<?php
include_once "./vendors/smarty.class.php";
$s = new smarty();
$s->template_dir='./template/';
$hello = "helloworld smarty";
$arrtest =array('name'=>'ada','age'=>18,'gender'=>'男');
class Human{
    public $name = 'class';
}
$s->assign('Human',new Human);
$s->assign('hello',$hello);
$s->assign('arrtest',$arrtest);
$s->display('01smarty.html');
?>
<html>
    <header></header>
    <body>
        <!--smarty定义占位符-->
        {$hello}
        <br/>
        姓名:{$arrtest['name']}&nbsp;&nbsp;
        年纪:{$arrtest.age}&nbsp;&nbsp;
        性别:{$arrtest.gender}
        <br/>
        Humanclass is {$Human->name}
        <!--smarty区分大小写-->
    </body>
</html>

PHP变量的值通过assign方法分配给smaety系统 模版中占位符无论是哪基础数据 数组 还是对象都是{$name}这种形式 只不过数组可以通过 . [] 得到值 对象通过 -> ::得到值
smarty保留变量
smarty保留变量对应PHP的预定义变量
$smarty.get .post .request .cookie .session .serviver .now当前时间戳
current_dir 当前路径 template模版文件 config配置信息
smarty保留变量可以访问到对应php预定义变量中的占值

<?php
include_once "./vendors/smarty.class.php";
$s = new smarty();
$s->template_dir='./template/';
$_GET['username'] = 'bob';
$s->display('01smarty.html');
?>
<html>
    <header></header>
    <body>
       <!--测试smarty保留变量-->
       username is {$smarty.get.username}
    </body>
</html>

自定义变量
上面两种方式 模版中的值 都是由PHP分配的
smaety支持直接在模版中分配数据 但是不常用
{assign var=‘名字’ vlaue='值‘ }

<html>
    <header></header>
    <body>
       <!--测试smarty自定义变量-->
        {assign var="name" value="condy"}
        自定义变量测试的是{$name}
    </body>
</html>
<?php
include_once "./vendors/smarty.class.php";
$s = new smarty();
$s->template_dir='./template/';
$s->display('01smarty.html');
?>

配置文件
smarty还支持通过加载配置文件来获取变量
我们在根目录中新建了一个smarty_config目录
在文件夹中新建了config.txt作为配置文件

<html>
    <header>{config_load file="../smarty_config/config.txt"}</header>
    <body bgcolor={$smarty.config.bgcolor}>
       <!--测试smarty配置文件-->
    </body>
</html>
<?php
include_once "./vendors/smarty.class.php";
$s = new smarty();
$s->template_dir='./template/';
$s->display('01smarty.html');
?>

bgcolor=#FFOOOO;

内置函数
当在模版中过的数据需要一定的逻辑处理时 smarty模版技术封装了一套支持分支循环的语法格式
分支结构
{if 判断语句} 执行语句 { else if} 执行语句 {/if}

<html>
    <header></header>
    <body>
       <!--测试内置函数 分支结构-->
       {if isset($smarty.get.username)}
       username is {$smarty.get.username}
       {else}
       nono 
       {/if}
    </body>
</html>

循环结构
foreach

<html>
    <header></header>
    <body>
      <!--测试foreach-->
      {assign var="user" value=array(111,222,333,444,555)}
      {foreach from=$user key='k' item='v'}
      {$k}:{$v}<br/>
      {/foreach}
    </body>
</html>

{foreach from=“数组名“ key=“k” k以后就代表键 item=’'v” v以后就代表值}
smarty为foreach提供了很多可访问的属性
$smarty.foreach.foreach名字.属性
$value@属性
key 下标 index索引 interation 当前次数 first的首次执行 last最后一次执行 show循环是否执行
total总次数

<html>
    <header></header>
    <body>
      <!--测试foreach-->
      {assign var="user" value=array('name','age','gender')}
      <table border=1>
        {foreach $user as $value}
        {if $value@first}第一次执行
            <tr>
                <th>下标</th>
                <th>循环次数</th>
                <th>值</th>
            </tr>
        {/if}
        <tr>
            <th>{$value@index}</th>
            <th>{$value@iteration}</th>
            <th>{$value}</th>
        </tr>
        {/foreach}
        {if $value@show} 循环在执行  数据为{$value@total}{/if}
      </table>
    </body>
</html>

smarty还为foreach提供了当foreach 遍历的数组没有数据

{assign var='arr' value=array()}
        {if $value@show} 循环在执行  数据为{$value@total}{/if}
        {foreach from=$arr key='key' item='value'}
        {$key}:{$value}
        {foreachelse}
        没有数据
        {/foreach}

文件包含
实际开发中会将模版拆分成多个

<div class='order'>
<!--菜单部分-->
</div>
<html>
<header></header>
<body>
<!--菜单部分-->
{include file='order.html'}
</body></html>

主要是利用{include file=dir}包含文件来实现一种分离思想
外部函数
当有些业务smarty内置函数无法解决时 smarty提供了一种自己写入代码的机制
1 系统函数的使用在模版中直接使用
{strlen('asdasa')}
2 自定义函数
先在php文件中定义函数function showa(){ echo 'a';}
直接在模版中使用{showa()}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值