[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
下一步在test项目里建立index.php
然后在templates模板目录里建立index.html
输出:
更上一层楼
【输出一维数组】
在index.php定义
在index.html里遍历
模板输出:12345
【输出二维数组】
在index.php定义
在index.html模板遍历
【section遍历二维数组】
如果是多维数组 name('favorite'=>array('sport'=>'basketball'))
其他
【变量调节】
【文件引入】
【在literal标签的内容不解析,正常使用】
常用的差不多就这么多了,想更深入的了解,看学习网站 http://www.yiibai.com/smarty
谢谢关注!
使用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
谢谢关注!
相关文章推荐
- php大力力 [001节]2015-08-21.php在百度文库的几个基础教程新手上路日记 大力力php 大力同学 2015-08-21 15:28
- 【5】说说Laravel5的blade模板
- 动态改变leftpanel的宽度
- phpcms v9 企业黄页加入企业会员提示“请选择企业库类型!”
- win7下安装matplotlib+six+pysparsing
- poi 中获得行数 getLastRowNum() 和 getPhysicalNumberOfRows()
- yii基础应用目录结构
- wamp,xampp,phpStudy等在安装拓展的时候出现的问题
- yii2.0发送邮件发不出去问题记录
- php 用命令行导出和导入MySQL数据库
- bigpipe+curl(php)
- php中 自己写的各种实用正则表达式-实时更新 2015-08-21 15:30:43
- phpci发送邮件
- windows下PHP环境的搭建
- 五种常见的 PHP 设计模式
- PHP正则匹配替换图片地址
- php+ci对mysql进行增删改查
- YII使用PHPExcel导入Excel文件的方法
- Yii: 扩展CGridView增加导出CSV功能
- thinkphp3.2 删除Runtime目录里的缓存文件,标记一下,以下好找。