设为首页 收藏本站
查看: 1675|回复: 0

[经验分享] php学习笔记(三十六)smarty中使用自定函数和代码块(smarty2和smarty3的不同)

[复制链接]

尚未签到

发表于 2017-4-13 10:44:18 | 显示全部楼层 |阅读模式
  

  

  初始化
  init.smarty.php

<?php
define("ROOT", ".");
//解决问题:Warning: strftime() [function.strftime]:
date_default_timezone_set("Asia/Shanghai");
include ROOT."/libs/Smarty.class.php";
$tpl = new Smarty();
//smarty初始化
$tpl->template_dir=ROOT."/templates/";
$tpl->compile_dir=ROOT."/templates_c/";
//配置文件位置
$tpl->config_dir=ROOT."/configs/";
$tpl->left_delimiter="<!--{";
$tpl->right_delimiter="}-->";
?>

一:在php文件中定义函数和块的使用  mysmarty.php


<?php
/**
*
* 以下所有使用变量都要基于设定的前缀和后缀<!--{}-->
* 函数使用
*
* 1.在Smarty中是以自定义标记(类似于html标记)的使用形式,来使用函数
*
* 2.使用smarty函数
* 2.1单行标记
* php中
* //smarty2的方式
* $tpl->register_function("hello","say");
* //smarty3的方式
* $tpl->registerPlugin("function","hello","say");
* 模板中调用
* <!--{hello size=5 color="green" content="22222" num=5 }-->
* 2.2块标记
* php中
* //smarty2的方式
* $tpl->register_block("world","test");
* //smarty3的方式
* $tpl->registerPlugin("block","world","test");
* 模板中调用
* <!--{world size=8 color="red" num=4 }-->
* world<!--{$title}--><br>
* <!--{/world}-->
* 3.传值
* php
* $tpl->assign("title",$title);
* $tpl->assign("color","pink");
* $tpl->assign("content","content");
* 模板(如果是关联数组需要使用1旁边的反引号来·来讲关联变量包含来进行先解析)
* 可以在页面中使用$color和$content
* <!--{hello size=5 color=$color content="$content" num=5 }-->
* <!--{hello size=5 color=$color content="`$arr.one`" num=5 }-->
*
*
*/
//如果文件加载失败require会停止继续解析php;而include则会继续向下执行
require 'init.smarty.php';
$title="这是一个文字标题";
$tpl->assign("title",$title);
$tpl->assign("color","pink");
$tpl->assign("content","content");
$tpl->assign("arr",array("one"=>"arr"));
//注册单行函数
//smarty2的方式
//$tpl->register_function("hello","say");
//$tpl->register_block("world","test");
//smarty3的方式
$tpl->registerPlugin("function","hello","say");
//注册块
$tpl->registerPlugin("block","world","test");
/**
* 定义函数,参数为数组放置标签的属性
* @param 传递参数 $args
*/
function say($args){
$html="";
for ($i=0;$i<$args["num"];$i++){
$html.='<font size=">'.$args["size"].'" color="'.$args["color"].'">'.$args["content"]."</font><br>";
}
return $html;
}
/**
* 定义块,参数为数组放置标签的属性
* @param 传递参数 $args
* @param 传递内容 $content
* @param 预留引用变量 $a
* @param 预留引用变量 $b
*/
function test($args,$content,&$a,&$b){
$html="";
for ($i=0;$i<$args["num"];$i++){
$html.='<font size=">'.$args["size"].'" color="'.$args["color"].'">'.$content."</font><br>";
}
return $html;
}
//模板文件名可以随便定义:比如:mysmarty.tpl只有内容是html就可以了
$tpl->display("mysmarty.html");
?>


mysmarty.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><!--{$title}--></title>
</head>
<body>
<!--{hello size=5 color=$color content="$content" num=5 }-->
<!--{hello size=5 color=$color content="`$arr.one`" num=5 }-->
<!--{world size=8 color="red" num=4 }-->
world<!--{$title}--><br>
<!--{/world}-->
</body>
</html>

二:在smarty插件文件夹plugins中加入新文件定义函数和块  mysmarty.php

<?php
/**
*
* 以下所有使用变量都要基于设定的前缀和后缀<!--{}-->
* 函数使用
*
* 插件形式写函数
* 在plugins文件夹来定义
* 以function开头的表示函数:function.hello.php的函数名smarty_function_hello($args,$smarty)
* 以block开头的表示块:block.world.php的函数名smarty_block_world($args,$content,&$smarty)
*
*
*/
//如果文件加载失败require会停止继续解析php;而include则会继续向下执行
require 'init.smarty.php';
$title="这是一个文字标题";
$tpl->assign("title",$title);
$tpl->assign("color","pink");
$tpl->assign("content","content");
$tpl->assign("arr",array("one"=>"arr"));
//模板文件名可以随便定义:比如:mysmarty2.tpl只有内容是html就可以了
$tpl->display("mysmarty2.html");
?>


mysmarty.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><!--{$title}--></title>
</head>
<body>
<!-- smarty插件文件夹中的自定义函数 -->
<!--{html_select_date start_year="1980" end_year="2020"}-->
<br>
<!--{hello size=5 color=$color content="$content" num=5 }-->
<!--{hello size=5 color=$color content="`$arr.one`" num=5 }-->
<!--{world size=8 color="red" num=4 }-->
world<!--{$title}--><br>
<!--{/world}-->

</body>
</html>


在smarty插件文件夹plugins下的:  function.hello.php(注意命名规则)

<?php
/**
* 定义函数,参数为数组放置标签的属性
* @param 传递参数 $args
*/
function smarty_function_hello($args,&$smarty){
$html="";
for ($i=0;$i<$args["num"];$i++){
$html.='<font size=">'.$args["size"].'" color="'.$args["color"].'">'.$args["content"]."</font><br>";
}
return $html;
}
?>


block.world.php(注意命名规则)
<?php
/**
* 定义块,参数为数组放置标签的属性
* @param 传递参数 $args
* @param 传递内容 $content
* @param 预留引用变量 $a
* @param 预留引用变量 $b
*/
function smarty_block_world($args,$content,&$smarty){
$html="";
for ($i=0;$i<$args["num"];$i++){
$html.='<font size=">'.$args["size"].'" color="'.$args["color"].'">'.$content."</font><br>";
}
return $html;
}
?>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-364341-1-1.html 上篇帖子: 83男人节基情发布 BF框架1.0Release 超超超轻量级PHP框架 下篇帖子: PHP与正则表达式 2 :一些修饰符与preg_match_all
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表