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

Smarty运用,赋值数组,定界符冲突

2015-10-18 13:03 573 查看
下载smarty,www.smarty.net  版本3.1.27

<?php
/*用真正的smarty模板*/
//引入类
require('../../smarty3/libs/Smarty.class.php');
//实例化
$smarty=new Smarty();
//配置路径
$smarty->template_dir='./temp';
$smarty->compile_dir='./comp';

$title='模板1';
$cont='smarty准备中...驾驶员认证中...启动!';
//赋值
$smarty->assign('title',$title);
$smarty->assign('cont',$cont);

//assign还可传数组,索引关联都可以
$info=array('name'=>'gintoki','age'=>'18','1'=>'01');
$smarty->assign('info',$info);

//编译
$smarty->display('temp1.html');

/*smarty和html 的css{}冲突解决方法1 修改定界符*/
/*$smarty->left_delimiter='{<';
$smarty->right_delimiter='>}';*/
/*冲突方法解决2 看temp1.html*/

?>

模板文件

temp1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{$title}</title>
</head>

<style type="text/css">
/*如果css换行没有冲突,如果一行可以用{literal}标签【是大括号*/
{literal}
table{color: red;}
{/literal}
</style>

<body>
<h1>{$cont}</h1>
<p>smarty可赋单个值,也可赋数组
模板里数组格式:关联数组$info.name 索引$info[1]
</p>
<table>
<tr><td>姓名</td><td>{$info.name}</td></tr>
<tr><td>年龄</td><td>{$info.age}</td></tr>
<tr><td>编号</td><td>{$info[1]}</td></tr>
</table>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php smarty 数组