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}

获取参数值 


var vid = "{/literal}{$smarty.get.v}{literal}";




Smarty载入外部文件方法 


      Smarty模版还可以包含其他文件,这里的其他文件包括其他静态HTML 文件以及其他模版文件。由于Smarty 模版可以接收来自PHP 代码的变量设定,一个模版文件还可以根据需要动态包含不同的文件。Smarty 模版包含文件的语法格式如下所示:


{include file=$filename variable=$value …}


其中,$filename 是要包含的文件的文件名,variable 和value 是用于替换被包含文件中关键字的变量设定。

一、以下代码是一个简单的包含文件的例子,其中test.htm 是模版文件,inc.htm 是被包含的一个静态HTML 文件。

  1. <?php  
  2. require 'libs/Smarty.class.php'; //包含Smarty 类库文件  
  3. $smarty = new Smarty; //创建一个新的Smarty 对象  
  4. $smarty->display('test7.htm'); //显示页面  
  5. ?> 


模版文件的HTML 代码如下所示:
 

<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代码如下:


 

  1. <?php  
  2. require 'libs/Smarty.class.php'; //包含Smarty 类库文件  
  3. $smarty = new Smarty; //创建一个新的Smarty 对象  
  4. $inc_name = "inc.htm";  
  5. $smarty->assign("inc_name",$inc_name); //对模版中的变量赋值  
  6. $smarty->display('test8.htm'); //显示页面  
  7. ?> 


模板文件如下:


<html>
<head>
<title>Smarty Test</title>
</head>
<body>
{include file=$inc_name}
</body>
</html>
 

 


运行结果与前面相同。


三、如果被包含的文件是模版,则需要使用调用该模版文件的模版来指定其中的变量。将前面的inc.htm改写如下:


{$sentence}

 

  1. <?php  
  2. require 'libs/Smarty.class.php'; //包含Smarty 类库文件  
  3. $smarty = new Smarty; //创建一个新的Smarty 对象  
  4. $inc_name = "inc.htm";  
  5. $sentence = "This is a test!";  
  6. $smarty->assign("inc_name",$inc_name); //对模版中的变量赋值  
  7. $smarty->assign("sentence",$sentence); //对模版中的变量赋值  
  8. $smarty->display('test.htm'); //显示页面  
  9. ?> 


模版文件如下所示:


<html>
<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>

----------------------------

<?php
   include_once("libs/Smarty.class.php");   // 包含smarty类文件
   $smarty=new Smarty();   // 建立smarty实例对象$smarty
   
   
   /*
   $smarty->caching=false;  // 是否使用缓存
   $smarty->template_dir="./templates/";  // 设置模版目录
   $smarty->compile_dir="./templates_c/";  //设置编译目录
   $smarty->cache_dir="./smarty_cache/";  // 缓存文件夹
   */
   //  设置作用边界符  默认是{  }  一般用<{ }>
   $smarty->left_delimiter="<{";
   $smarty->right_delimiter="}>";
   
   $smarty->assign("aa","hello");
   $smarty->assign("bb",567);
   $smarty->assign("cc",56.7);
   $smarty->assign("dd",true);
   
   // 1.一维数组的索引数组
   $arr1 = array('北京','上海','天津');
   //2.一维数组的关联数组
   $arr2 = array('city1'=>'北京','city2'=>'上海','city3'=>'天津');
   //3.二维数组的索引数组
   $arr3 = array(array('北京','上海','天津'),array('1111','2222','3333'));
   //4.二维数组的关联数组
   $arr4 = array(array('city1'=>'北京','city2'=>'上海','city3'=>'天津'),array('name'=>'xiao','age'=>60,'city'=>'天津'));
   
   $arr5 = array('emp1'=>array('name1'=>'xiao1','age1'=>601,'city1'=>'天津1'),'emp2'=>array('name2'=>'xiao2','age2'=>602,'city2'=>'天津2'));
   
   $smarty->assign("arr1",$arr1);
   $smarty->assign("arr2",$arr2);
   $smarty->assign("arr3",$arr3);
   $smarty->assign("arr4",$arr4);
   $smarty->assign("arr5",$arr5);
   
   $smarty->display("hello.tpl");
     
?>


<{config_load file='../config/my.conf'}>
<!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>
<{*  from是需要循环的数组名字  item是当前元素的变量名   key当前关键字的变量名 *}>
<{foreach from=$arr2 item=temp key=k}>
<{$k}>=<{$temp}>
<{/foreach}>

<h1>循环取出二维索引数组</h1>
<{foreach from=$arr3 item=temp}>
<{foreach from=$temp item=val key=k}>
<{$val}>
<{/foreach}>
<br />
<{/foreach}> 

<h1>取出二维的关联数组</h1>
<{foreach from=$arr4 item=temp}>
<{*  全部取出这里需要再次循环 *}>
<!--<{foreach from=$temp item=val key=k}>
<{$k}>=<{$val}>
<{/foreach}>-->
<{取出具体的某一项这里只需要一次循环}>
<{$temp.city}>
<{$temp.name}>
<br />
<{/foreach}> 
</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}的使用详解 


0 0