Smarty相关
来源:互联网 发布:软件开发需求阶段 编辑:程序博客网 时间:2024/06/08 09:33
Smarty中{literal}的使用详解
{literal}
<script>
function Login(){
document.LoginForm.submit();
}
</script>
{/literal}
===============================================================================
Literal 标签区域内的数据将被当作文本处理,此时模板将忽略其内部的所有字符信息. 该特性用于显示有可能包含大括号等字符信息的 javascript 脚本. 当这些信息处于 {literal}{/literal} 标签中时,模板引擎将不分析它们,而直接显示.
===============================================================================
例子:
{literal}
<script language=javascript>
<!--
function isblank(field) {
if (field.value == '')
{ return false; }
else
{
document.loginform.submit();
return true;
}
}
// -->
</script>
{/literal}
获取参数值
Smarty载入外部文件方法
Smarty模版还可以包含其他文件,这里的其他文件包括其他静态HTML 文件以及其他模版文件。由于Smarty 模版可以接收来自PHP 代码的变量设定,一个模版文件还可以根据需要动态包含不同的文件。Smarty 模版包含文件的语法格式如下所示:
{include file=$filename variable=$value …}
其中,$filename 是要包含的文件的文件名,variable 和value 是用于替换被包含文件中关键字的变量设定。
一、以下代码是一个简单的包含文件的例子,其中test.htm 是模版文件,inc.htm 是被包含的一个静态HTML 文件。
- <?php
- require 'libs/Smarty.class.php'; //包含Smarty 类库文件
- $smarty = new Smarty; //创建一个新的Smarty 对象
- $smarty->display('test7.htm'); //显示页面
- ?>
模版文件的HTML 代码如下所示:
<head>
<title>Smarty Test</title>
</head>
<body>
{include file="inc.htm"}
</body>
</html>
被包含的文件inc.htm 代码如下所示。
This is a test!
运行结果如下所示:
This is a test!
可以看到,被包含的文件内容被显示出来了。
二、被包含的文件名还可以通过PHP 代码动态指定,下面将上面的示例代码如下:
PHP代码如下:
- <?php
- require 'libs/Smarty.class.php'; //包含Smarty 类库文件
- $smarty = new Smarty; //创建一个新的Smarty 对象
- $inc_name = "inc.htm";
- $smarty->assign("inc_name",$inc_name); //对模版中的变量赋值
- $smarty->display('test8.htm'); //显示页面
- ?>
模板文件如下:
<head>
<title>Smarty Test</title>
</head>
<body>
{include file=$inc_name}
</body>
</html>
运行结果与前面相同。
三、如果被包含的文件是模版,则需要使用调用该模版文件的模版来指定其中的变量。将前面的inc.htm改写如下:
{$sentence}
- <?php
- require 'libs/Smarty.class.php'; //包含Smarty 类库文件
- $smarty = new Smarty; //创建一个新的Smarty 对象
- $inc_name = "inc.htm";
- $sentence = "This is a test!";
- $smarty->assign("inc_name",$inc_name); //对模版中的变量赋值
- $smarty->assign("sentence",$sentence); //对模版中的变量赋值
- $smarty->display('test.htm'); //显示页面
- ?>
模版文件如下所示:
<head>
<title>Smarty Test</title>
</head>
<body>
{include file=$inc_name sentence=$sentence}
</body>
</html>
可以看到上面的例子将一个变量$sentence 从PHP 代码传入模版文件中,又通过模板文件传入inc.htm 中,实现了与前面例子相同的功能。
数组
smarty数组遍历
一、foreach和section遍历数组的内容
foreach:
语法:<{foreach name from item key }>语句<{foreachelse}>语句2<{/foreach}>
key:当前元素的键值 name:当前循环的名字
form:循环数组的名称 item:当前处理元素的变量名称
section:
语法:<{section}> <{/section}>
name :该循环的名称(自定义)
loop:决定循环次数的变量名称(数组名)
section不能遍历关联数组
if语句:
<{if 条件}> 语句1<{elseif 条件2}>语句2<{else}>语句3<{/if}>
二、把数组以表格的形式输出
1、
<?php
include("libs/Smarty.class.php");
$smarty=new Smarty();//创建smarty对象
$smarty->template_dir="demo/templates";//修改模板存放路径
$smarty->compile_dir="demo/tempaltes_c";//修改编译路径
$smarty->left_delimiter="<{";//修改定界符
$smarty->right_delimiter="}>";
$arr=array(
array("id"=>"1","name"=>"张三","age"=>18),
array("id"=>"2","name"=>"李四","age"=>"19")
);
$ar=array(
array("1","张三",18),
array("2","李四","19")
);
$smarty->assign("arr",$arr);//给模板中的变量赋值
$smarty->assign("ar",$ar);
$smarty->display("test3.tpl");
?>
2、test3.tpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<table width="200" border="1">
<caption>一个一个的写值</caption>
<th>id</th><th>name</th><th>age</th>
<tr>
<td><{$arr[0].id}></td>
<td><{$arr[0].name}></td>
<td><{$arr[0].age}></td>
</tr>
<tr>
<td><{$arr[1].id}></td>
<td><{$arr[1].name}></td>
<td><{$arr[1].age}></td>
</tr>
</table>
<br>
<table width="200" border="1">
<caption>foreach遍历一条</caption>
<th>id</th><th>name</th><th>age</th>
<tr>
<{foreach from=$arr[0] item=value}>
<td><{$value}></td>
<{/foreach}>
</tr>
</table>
<br>
<table width="200" border="1">
<caption>foreach遍历全部</caption>
<th>id</th><th>name</th><th>age</th>
<{foreach from=$arr item=row }><!--row=$row[0],$row[1]..,获得外层所有的数组,都遍历出来-->
<tr>
<{foreach from=$row item=value}><!--$row是一个数组,在获得里面的值,value=1,张三,18-->
<td><{$value}></td>
<{/foreach}>
</tr>
<{/foreach}>
</table>
<br>
<table width="200" border="1">
<caption>section遍历全部</caption>
<th>id</th><th>name</th><th>age</th>
<tr>
<{section name=loop_a loop=$arr}><{*loop决定循环次数的变量名称,已经决定次数*}>
<td><{$arr[loop_a].id}></td>
<td><{$arr[loop_a].name}></td>
<td><{$arr[loop_a].age}></td></tr>
<{/section}>
</table>
<br>
<table width="200" border="1">
<caption>section遍历一条</caption>
<th>id</th><th>name</th><th>age</th>
<tr>
<{section name=loop_a loop=$ar[1]}>
<td><{$ar[1][loop_a]}></td><{*section不能遍历关联数组*}>
<{/section}>
</tr>
</table>
<br>
<table width="200" border="1">
<caption>section遍历索引全部</caption>
<th>id</th><th>name</th><th>age</th>
<{section name=row loop=$ar}>
<tr>
<{section name=col loop=[row]}>
<td><{$ar[row][col]}></td>
<{/section}>
</tr>
<{/section}>
</table>
</body>
</html>
三、if elseif else,如果选择左边把你输入的内容显示在左边,如果选择是右把你输入的内容显示在右边。。。
1、if.html
<body>
<form action="if.php" method="post">
<input type="text" name="text" value="aaa">
<input type="radio" value="lift"name="radio">lift
<input type="radio" value="right" name="radio">right
<input type="radio" value="center" name="radio">center
<input type="submit" value="提交">
</form>
2、if.php
<?php
include("libs/Smarty.class.php");
$smarty=new Smarty();
$smarty->template_dir="demo/templates";
$smarty->compile_dir="demo/tempaltes_c";
//$smarty->lift_delimiter="<{";
//$smarty->right_delimiter="";
$smarty->assign("test",$_POST["text"]);
$smarty->assign("radio",$_POST["radio"]);
$smarty->display("if.tpl");
?>
3、if.tpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
{if $radio=='lift'}
<span style="float:left;"> {$test} </span>
{elseif $radio=='right'}
<span style="float:right;">{$test}</span>
{else}
<center>{$test} </center>
{/if}
</body>
</html>
----------------------------
assign 赋值
void assign (mixed var)
void assign (string varname, mixed var)
This is used to assign values to the templates. You can explicitly pass name/value pairs, or associative arrays containing the name/value pairs.
用来赋值到模板中。可以指定一对 名称/数值 ,也可以指定包含 名称/数值 的联合数组。
Example 13-3. assign
例子 13-3. 赋值
// passing name/value pairs 名称/数值 方式$smarty->assign("Name","Fred");$smarty->assign("Address",$address);// passing an associative array 联合数组方式$smarty->assign(array("city" => "Lincoln","state" => "Nebraska"));
Smarty中{literal}的使用详解
Smarty中{literal}的使用详解
Smarty中{literal}的使用详解
Smarty中{literal}的使用详解
Smarty中{literal}的使用详解
Smarty中{literal}的使用详解
- Smarty相关
- smarty相关
- smarty相关理解
- smarty 相关知识
- symfony 结合 smarty 及相关的问题
- Smarty
- Smarty
- smarty
- smarty
- Smarty
- smarty
- smarty
- smarty
- smarty
- smarty
- smarty、
- smarty
- smarty
- jQuery 实现广告条效果
- DirectX技术----D2D基础篇(一)
- BLE通信(续)
- string
- MongoDb的“not master and slaveok=false”错误及解决方法
- Smarty相关
- (4.1.27.7)JNI/NDK开发指南(四)——JNI 实战全面解析
- Node之XML
- HDU-3081-Marriage Match II【并查集】【二分图匹配】
- Android Material Design之Toolbar与Palette实践
- iOS学习之—— Notification的使用
- js获取当前时间和一星期前的时间
- android上传图片至服务器
- 通达OA2015集团版系列小正版补丁加注册授权文件