您的位置:首页 > 编程语言 > PHP开发

[php] smarty模板引擎

2015-08-21 15:33 736 查看
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

谢谢关注!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: