Smarty变量调节器(可以参考手册)

<?php
 require_once ‘libs/Smarty.class.php‘;
 $smarty=new Smarty;
 $smarty->left_delimiter="<{";
 $smarty->right_delimiter="}>";
 
 $smarty->assign("a","hello world");
 $smarty->assign("b","smarty");
 $smarty->assign("c","hello");
 $smarty->assign("d","hello \n world");
 $smarty->assign("e","hello. world.");
 $smarty->assign("f","hello world hello world.");
 $smarty->assign(‘yesterday‘,strtotime(‘0 day‘));  //可以定义
 $smarty->assign("title","ni hao");
 $smarty->assign("dz","<a href=‘http://www.baidu.com‘>百度</a>");
  $smarty->assign("con","ni\nhao");
 $smarty->assign("h","hello\nworld");
 $smarty->display("index.html");
?>

模版文件index.html:

<head>
<title>Smarty模版的变量调节器实例</title>
</head>
<h2>-----capitalize(首字母大写)---------</h2>
<{$a|capitalize}><br>
<h2>-----count_characters(字符计数)----</h2>
<{$b}>=<{$b|count_characters}>个字符
<h2>------cat(连接字符串)--------</h2>
<{$c|cat:world}>
<h2>-----count_paragraphs(计算段数)----(换行符\n)----</h2>
<{$d}><br>
<{$d|count_paragraphs}>
<h2>-------count_sentences(计算句数)------------</h2>
<{$e}><br>
<{$e|count_sentences}>
<h2>----count_words(计算词数)--------</h2>
<{$f}><br>
<{$f|count_words}>
<h2>------date_format(格式化日期)-----(区分大小写)--</h2>
<{$smarty.now|date_format}><br>
<{$smarty.now|date_format:"%y-%m-%d %H:%I:%S"}><br>
<{$yesterday|date_format}><br>
<{$yesterday|date_format:"%y-%m-%d %H:%I:%S"}><br>
<h2>-----default(默认值)-------------</h2>
<{$title|default:"no title"}><br>
<{$biaoti|default:"no biaoti"}>
<h2>------escape(编码)------</h2>
<{$dz}><br>
<{$dz|escape}>
<h2>------indent(首行缩进)------</h2>
<{$in}><br>
<{$in|indent}><br>
<{$in|indent:1:"\t"}>
<h2>------nl2br换行符替换成<br />--------</h2>
<{$con}><br>
<{$con|nl2br}>
<h2>-----------regex_replace 正则替换------ </h2>
<{$h}><br>
<{$h|regex_replace:"/[\r\t\n]/":" <br/> "}>
<h2>------replace(替换)------</h2>
<{$h|replace:"hello":"你好"}>
</html>

往往开发中有时候是满足不了需求的,那就可以直接写这些变量调节器,其实所谓的变量调节器就是一个函数,在这里我们自己建一个,首先在plugins文件夹里面间建立一个php文件,命名一定按着他的规范,。

modifier.capita.php

function Smarty_modifier_capita($string){   //函数命名也是有规范的
    return strtoupper(substr($string,0,1)).strtolower(substr($string,1));
  }
模版文件调用:

<{$a|capita}>

时间: 2024-08-06 15:47:25

Smarty变量调节器(可以参考手册)的相关文章

smarty变量调节器如何使用

smarty变量调节器是用来改变显示的值,不改变变量的值.这里给大家总结了一下,希望对大家的smarty学习有参考意义. 1.capitalize[首字符大写]  调用方法: <?php$smarty->assign('articleTitle', 'next x-men film, x3, delayed.');?> 调用方法: {$articleTitle} {$articleTitle|capitalize} {$articleTitle|capitalize:true} 实现结果

smarty 变量调节器

变量调节器:<{$a|变量调节器}> 了解更多可以查询smarty手册 主要修改此页面的信息来了解变量调节器:main.php/main.html(0603) 1.利用给定的变量调节器 capitalize 首字母大写 php页面:$smarty->assign("ceshi","hello world"); html页面:<div><{$ceshi|capitalize}></div> 运行后:Hello Wo

6月5日 smarty变量调节器 函数 配置文件

变量调节器: 1.capitalize[首字符大写] main.php: <?php include("../init.inc.php"); smarty->assign("attr","hello word");smarty->display("main.html"); main.html: <body> <div><{$attr|capitalize}></div

Smarty变量调节器(Linux 管道)

变量调节器:前者的输入是后者的输出 也就是函数使用 <{变量|函数方法名称}> 变量这里做为 函数的参数 输出的是执行后的结果 函数实例(变量调节器) {$name}<br /> {$name|lower} <br />{*把变量字符串转换为小写*} {$name|upper} <br />{*大写转换*} {$content}<br /> {$content|truncate:18}<br />{*对内容进行截取,汉字也是一个字符*

smarty变量调节器与函数

smarty自带了一些变量调节器与内置函数,都在libs/plugins目录下,变量调节器以modifier开头,函数以function开头,而且我们可以自定义变量调节器与函数,熟练运用之后会极大地提高我们的开发效率. 一.格式 自定义的时候必须严格按照smarty提供的书写格式: 1.自定义变量调节器: 文件名格式:modifier.方法名.php,如:modifier.fontcolor.php 方法格式:function smarty_modifier_方法名(参数,参数……){ 方法 }

smarty变量调节器

1.capitalize[首字符大写]  调用方法: <?php$smarty->assign('articleTitle', 'next x-men film, x3, delayed.');?> 调用方法: {$articleTitle} {$articleTitle|capitalize} {$articleTitle|capitalize:true} 实现结果: next x-men film, x3, delayed. Next X-Men Film, x3, Delayed.

smarty变量调节器案例

要求: 如下图,有内容的每一行,当鼠标放上去显示灰色区域,当鼠标离开灰色区域消失 说明: 循环实现行显示,循环体是一个div,里面放两个div,用js实现效果,获取到该元素,一上来每一行中的灰色区域设置隐藏,添加事件 $(".xs").mouseover(function(){ $(this).children("div").css("display","block"); }) : 代码: 后台 <?php includ

Smarty模板变量调节器

Smarty模板变量调节器用法 在smarty里面,怎么修饰文本和变量呢?当然,你可以通过php函数处理文本,然后再通过assign()方法分配到模板,其实smarty提供了变量调节器能够很容易的处理文本,语法方式如下: {$var|modifier1|modifier2|modifier3|…} 说明:1.不管应用了多少个函数,都应该以|来进行分离. 说明:2.参数用":"分开,如: {$str|truncate:40:"..."}//截取字符串长度为40,并以.

夺命雷公狗---Smarty NO:06 设计篇4 变量调节器

{$var|regex_replace:”/[\t\n]/”:””} :正则替换 {$var|replace:”aa”:”bb”} :字符替换 {$var|spacify:”^^”} :插空(在字符与字符之间插空) {$var|string_format:”%d”} :字符串格式化 %d:格式化为整数 %s :格式化字符串 {$var|strip: “*”} :去除多余的空格 {$var|strip_tags} :去除html标签 {$var|truncate:30:”…”} :截取指定长度的字