变量调节器:<{$a|变量调节器}>

了解更多可以查询smarty手册

主要修改此页面的信息来了解变量调节器:main.php/main.html(0603)

1.利用给定的变量调节器

capitalize 首字母大写

php页面:$smarty->assign("ceshi","hello world");

html页面:<div><{$ceshi|capitalize}></div>

运行后:Hello World

truncate截取字符串(有参数),10代表截取的长度, . 是第三个参数,代表剩余的内容 

 <td><{$v[1]|truncate:10:"..."}></td>    也可以用:<td><{$v[1]|truncate:10}></td>(默认是...)

运行后:                                                    原图:

           

2. 自己写的变量调节器

在lib/plugins中新建文件,命名方式是固定的:modifier.变量调节器名称.php

 2.1 改变答案的颜色

新建文件:modifoer.fontcolor.php

 <?php
//参数:第一个是默认的字符串$str,必须写
function smarty_modifier_fontcolor($str,$color="green")
{
return "<span style='color:{$color}'>$str</span>";
}

在前端调用方法main.html:    <td><{$v[2]|fontcolor:red}></td>

          

2.2 组合使用变量调节器:

在前端调用方法main.html:  <td><{$v[1]|truncate:10:"..."|fontcolor}></td>

2.3 显示所属科目的名称

新建文件:modifoer.kmname.php

 <?php
function smarty_modifier_kmname($str)
{
$conn=new MySQLi("localhost","root","","mydb");
$sql="select name from kemu where code = '{$str}'";
$result=$conn->Query($sql);
$attr=$result->fetch_row();
return $attr[0];
}

在前端调用方法main.html:    <td><{$v[3]|kmname}></td>

        

2.4 显示难度的名称

新建文件:modifoer.nandu.php

 <?php
function smarty_modifier_nandu($str)
{
if($str==0)
{
return "简单";
}
else if($str==1)
{
return "适中";
}
else
{
return "困难";
}
}

在前端调用方法main.html:   <td><{$v[4]|nandu}></td>

      

2.5 显示类型的名称

新建文件:modifoer.type.php

 <?php
function smarty_modifier_type($str)
{
if($str==0)
{
return "判断";
}
else if($str==1)
{
return "单选";
}
else
{
return "多选";
}
}

在前端调用方法main.html:    <td><{$v[5]|type}></td>

        

main.html 中全部修改内容:

<{foreach $shuju as $v}>
<tr>
<!--truncate截取字符串(有参数),10代表截取的长度,.是第三个参数,代表剩余的内容 -->
<td><{$v[1]|truncate:10:"..."|fontcolor}></td>
<td><{$v[2]|fontcolor:red}></td>
<td><{$v[3]|kmname}></td>
<td><{$v[4]|nandu}></td>
<td><{$v[5]|type}></td>
<td><a href="delete.php?code=<{$v[0]}>">删除</a>
<a href="update.php?code=<{$v[0]}>">修改</a>
</td>
</tr>
<{/foreach}>

 <!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>
<h1>主页面</h1>
<table width="70%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>题目名称</td>
<td>答案</td>
<td>所属科目</td>
<td>难度</td>
<td>类型</td>
<td>操作</td>
</tr> <{foreach $shuju as $v}>
<tr>
<!--truncate截取字符串(有参数),10代表截取的长度,.是第三个参数,代表剩余的内容 -->
<td><{$v[1]|truncate:10:"..."|fontcolor}></td>
<td><{$v[2]|fontcolor:red}></td>
<td><{$v[3]|kmname}></td>
<td><{$v[4]|nandu}></td>
<td><{$v[5]|type}></td>
<td><a href="delete.php?code=<{$v[0]}>">删除</a>
<a href="update.php?code=<{$v[0]}>">修改</a>
</td>
</tr>
<{/foreach}>
</table><br /> <a href="add.php">添加数据</a><br /> </body>
</html>

smarty 变量调节器的更多相关文章

  1. smarty变量调节器与函数

    smarty自带了一些变量调节器与内置函数,都在libs/plugins目录下,变量调节器以modifier开头,函数以function开头,而且我们可以自定义变量调节器与函数,熟练运用之后会极大地提 ...

  2. 6月5 Smarty变量调节器

    变量调节器:<{$a|变量调节器}> 主要修改此页面的信息来了解变量调节器:test0605/main.php和模板文件:main0605.html 1.利用给定的变量调节器 capita ...

  3. smarty变量调节器

    smarty中变量调解器的作用:在模板中需要对PHP分配过来的变量在输出之前,对变量进行处理 注册变量调解器方式:$smarty->registerPlugin("modifier&q ...

  4. smarty变量调节器案例

    要求: 如下图,有内容的每一行,当鼠标放上去显示灰色区域,当鼠标离开灰色区域消失

  5. 第八十八天请假 PHP smarty模板 变量调节器,方法和块函数基本书写格式

    变量调节器 : 文件命名格式(modifier.名称.php)  前端调用方式<{变量|名称:参数:参数……}>可组合使用,用|隔开 <?php /* 命名格式 smarty_mod ...

  6. Smarty模板变量调节器

    Smarty模板变量调节器用法 在smarty里面,怎么修饰文本和变量呢?当然,你可以通过php函数处理文本,然后再通过assign()方法分配到模板,其实smarty提供了变量调节器能够很容易的处理 ...

  7. smarty 模板几个例子(变量调节器)

    一.assign和display方法的使用以及几个变量调节器 header("content-type:text/html;charset=utf-8");//加载Smarty引擎 ...

  8. smarty学习——变量调节器(过滤器)

    变量调节器用于变量,自定义函数和字符串. 请使用 | 符号和调节器名称应用调节器.变量调节器由赋予的参数值决定其行为.参数由:符号分开. 比如进行大写转换的: upper demo: <br&g ...

  9. Smarty 变量使用

    Smarty的标签都是使用定界符括起来. 默认定界符是{ 和 }, 但定界符可以被改变. 比如说在本手册,我们会假定你在使用默认的定界符. 在Smarty看来,任何在定界符之外的内容,都是静态的,或者 ...

随机推荐

  1. android Base64加密解密

    // 加密传入的数据是byte类型的,并非使用decode方法将原始数据转二进制,String类型的数据 使用 str.getBytes()即可 String str = "Hello!&q ...

  2. 14.2 InnoDB and the ACID Model

    14.2 InnoDB and the ACID Model ACID 模型是一组数据库设计原则,强调可靠性方面对于商业数据和关键人物. MySQL 包含组件比如InnoDB存储引擎坚持ACID 模型 ...

  3. 7.3.1 Establishing a Backup Policy

    7.3 Example Backup and Recovery Strategy 备份和恢复策略实例 7.3.1 Establishing a Backup Policy 7.3.2 Using Ba ...

  4. MFC对话框应用程序添加自定义消息

    1. 定义自定义消息 /** * \brief 消息测试 */ #define E6100_MSG_TEST          ( WM_USER + 1001 ) 2. 声明自定义消息处理函数 /* ...

  5. Best Time to Buy and Sell Stock II ——LeetCode

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. HDU_1401——分步双向BFS,八进制位运算压缩,map存放hash

    Problem Description Solitaire is a game played on a chessboard 8x8. The rows and columns of the ches ...

  7. NSTemporaryDirectory 临时文件

    唯一标识 : NSString*identifier=[[NSProcessInfoprocessInfo]globallyUniqueString]; 创建临时文件路径: NSString *fil ...

  8. I - Strategic Game - hdu 1054(最小点覆盖)

    题意:用最小的点来覆盖全部的边,因为二分图里面最大的匹配就是最小覆盖,所以直接匹配一下即可 ****************************************************** ...

  9. Java程序员的日常—— 垃圾回收中引用类型的作用

    在Java里面,是不需要太过于关乎垃圾回收,但是这并不意味着开发者可以不了解垃圾回收的机制,况且在java中内存泄露也是家常便饭的事情.因此了解垃圾回收的相关知识就显得很重要了. 引用,在垃圾回收中是 ...

  10. 【AngularJS入门】用ng-repeat指令实现循环输出

    循环输出列表很多项目在web服务端做,前端做好模版后后端写jsp代码,双方需要紧密合作,分清责任.有些项目由后端提供restful方法,前端用ajax调用自己循环,这种一般是大把的jquery拼字符串 ...