|
初始化
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;
}
?> |
|
|