[php] smarty模板引擎

Smarty是PHP的"半官方"的模板化引擎,从其主页的位置就可以看出。Smarty的作者是Andrei Zmievski和Monte Orte。它是在GNU宽通用公共许可(LGPL)下发布的,可能是最流行、功能最强大的PHP模板化引擎。

 

使用smarty模板需要去下载smarty官网下载地址:http://www.smarty.net/download ,在线手册:http://smarty.jz123.cn/  http://www.yiibai.com/smarty/ (推荐)

 

smarty案例:http://pan.baidu.com/s/1kTndY5L

 

先来入门,输出hello world

首先建立一个test项目,然后把下载的smarty里的libs目录文件拷贝到test项目里并重命名为smarty

接着在test项目里建立config.php

 

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/21
 * Time: 11:18
 */

include_once('./smarty/Smarty.class.php');

$smarty = new Smarty();

// 如果不配置这三个目录,smarty会自动建立
$smarty -> template_dir = "./templates"; //设置模板目录
$smarty -> compile_dir = "./templates_c"; //设置编译目录
$smarty -> cache_dir = "./cache"; //缓存文件夹,开启缓存后会在这个目录生成缓存文件

$smarty->debugging = true;  // 开启调试
$smarty->caching = true; // 开启缓存
$smarty->cache_lifetime = 120;  // 缓存有效期

$smarty->left_delimiter = '{#';  // 左定界符
$smarty->right_delimiter = '#}';  // 右定界符

 

 

下一步在test项目里建立index.php

 

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/21
 * Time: 11:27
 */
include "config.php";

$smarty->assign('word', 'this is a word');

$smarty->display('index.html');

 

 

 

然后在templates模板目录里建立index.html

 

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    hello world !
    <hr/>
    {#$word#}
</body>
</html>

 

 

 

输出:

 

更上一层楼

【输出一维数组】

在index.php定义

 

$data = array(1,2,3,4,5);
$smarty->assign('arr', $data);

 

 

 

在index.html里遍历

 

{#foreach from=$arr item=v#}
    {#$v#}
{#/foreach#}

 

模板输出:12345

 

【输出二维数组】

在index.php定义

 

$data2 = array(
    array("name"=>"张三", "age"=>24),
    array("name"=>"李四", "age"=>18),
    array("name"=>"王二", "age"=>34),
    array("name"=>"麻子", "age"=>26),
);

$smarty->assign('employee', $data2);

 

 

 

在index.html模板遍历

 

<table border="1">
    <thead>
        <tr>
            <th>姓名</th>
            <th>年龄</th>
        </tr>
    </thead>
    <tbody>
        {#foreach from=$employee key=k item=value#}
            {#if is_array($value)#}
                <tr>
                    {#foreach from=$value key=tk item=tv #}
                        <td>{#$tv#}</td>
                    {#/foreach#}
                </tr>
            {#/if#}
        {#/foreach#}
    </tbody>
</table>

 

 

 

【section遍历二维数组】

 

<table border="1">
    <thead>
    <tr>
        <th>姓名</th>
        <th>年龄</th>
    </tr>
    </thead>
    <tbody>
        {#section name=employee loop=$employee#}
            <tr>
                <td>{#$employee[employee].name#}</td>
                <td>{#$employee[employee].age#}</td>
            </tr>
        {#/section#}
    </tbody>
</table>

 

 

 

如果是多维数组 name('favorite'=>array('sport'=>'basketball'))

 

<td>{#$employee[employee].name.favorite.sport#}</td>

 

 

其他

【变量调节】

 

{#$date|date_format:"%Y-%m-%d"#}

 

 

 

【文件引入】

 

{#include file="header.html"#}

 

 

 

 

{#include_php file="/path/to/load_nav.php"#}

 

 

【在literal标签的内容不解析,正常使用】

 

{#literal#}
<script>……</script>
{#/literal#}

 

 

 

常用的差不多就这么多了,想更深入的了解,看学习网站 http://www.yiibai.com/smarty

欢迎关注技术开发分享录:http://fenxianglu.cn/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天空还下着雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值