smarty--初级

2017.2.29-2012.2.29

什么是smarty

是模板(html+css)引擎技术之一
可以使得“php代码”与“html代码”分离的技术都 称为模板引擎技术

官网

http://www.smarty.net/

目录结构

第一个例子

目录结构

  1. 将上述的libs文件将复制到项目目录下
  2. 创建php文件,demo.php

    <?php 
    //引入核心类
    include "./libs/Smarty.class.php";
    //实例化
    $smarty=new Smarty;
    //将text设置为smarty对象属性的一部分
    $smarty->assign('text','hello world!');
    //展示模板
    $smarty->display("demo.html");
    
  3. 创建template文件夹,在template文件夹下创建demo.html模板文件

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        姓名:{$text}
    </body>
    </html>
    

    4.浏览器访问,访问后会产生一个templates_c文件,这是php与html的混编文件。

设置路径

$smarty->setTemplateDir("./View/");            //设置模板路径
$smarty->setCompileDir("./View_c/");           //设置混编文件路径
$smarty->left_delimiter="<@";          //设置模板变量的左分界符
$smarty->right_delimiter="@>";         //设置模板变量的右分界符

注意:在smarty模板中不能直接写PHP代码,即在View目录的html文件中不能写php代码

$smarty变量的应用

可在模板中使用$smarty.get.name,获得get方式的参数name,同样还有

- smarty.get. smarty.post.*
- smarty.session. smarty.cookies.*
- smarty.request. smarty.server.*
- $smarty.env.*

配置变量的使用

  1. 在根目录下创建configs文件夹,在configs文件夹下创建site.conf文件(site可换成别的)
  2. 在site.conf文件里写上配置常量

    FIRST=hello world!
    NAME=zcw
    
  3. 在模板文件里引入,代码如下

    {config_load file="site.conf"}
    
  4. 在模板文件中调用常量

    {#NAME#}
    或者
    {$smarty.config.NAME}
    

配置变量段(section)的使用

  1. 配置文件中写上段

    [zcw]
    FIRST=hello world!
    NAME=zcw
    
    [hy]
    FIRST=hello!
    NAME=hy
    
  2. 模板文件中写上段名

    {config_load file="site.conf" section="hy"}
    

{}使用与css或js内容有冲突

解决办法:
1. 给{}标记开始和结束加上空格
2. {literal}{/literal}里的内容不会给smarty解析

遍历数组

语法格式:
{foreach 数组 as 下标变量 => 值变量}
    //具体操作
{foreachelse}
    each '数组没有任何元素'
{/foreach}

值变量@iteration,从1开始
值变量@index,从0开始
值变量@first,判断是否为第一个元素
值变量@last,判断是否为最后一个元素
值变量@total,获取数组的长度(个数)
值变量@show,判断当前数组是否有遍历出元素来

分支结构

  • 单路分支

    {if 条件}
        分支逻辑
    {/if}
    
  • 双路分支

    {if 条件}
    {else}
    {/if}
    
  • 多路分支

    {if 条件}
    {elseif 条件}
    {elseif 条件}
    {else}
    {/if}
    

复选框

语法格式:   {html_checkboxes name="animal" options=$animal seperator="/n"  selected=$sel}
参数解释:
option:是一个关联数组,键为复选框的值,值为复选框的文字
seperator:复选框的间隔符
selected:默认选中的,形式为以复选框的值为元素的数组

下拉框

{html_options name="animal" options=$animal selected=$sel multiple="multiple"}
参数解释:
option:是一个关联数组,键为下拉框的值,值为下拉框的文字
selected:默认选中的,形式为以下拉框的值为元素的数组
multiple:是否可以选中多个

布局继承使用

  1. 创建布局文件,如layout.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>{block name="title"}{/block}</title>
    </head>
    <body>
        <div>导航</div>
        {block name="main"}{/block}
        <div>尾部</div>
    </body>
    </html>     
    
  2. 引入布局文件

    {extends file="layout.html"}
    {block name="title"}demo{/block}
    {block name="main"}<div>主要部分</div>{/block}
    

变量调节器

  • date_format[格式化日期]
  • default[默认值]
  • lower[小写]
  • truncate[截取]
  • upper[大写]

缓存

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值