我们接着上次的内容讲解,首先这次把smarty定界符改了:
$tpl->left_delimiter = "<!--
{";//左定界符 $tpl->right_delimiter = "}-->";//右定界符
2.变量
下面是通过php赋值(assign)方式分配值: 1.)简单变量
通过
$smarty->assign('name','xcf007'); 分配的变量,在模板里面我们可以像php中那样调用
如
<!--{$name}--> 2.)数组
这是简单的变量形式,下面看看数组的:
关键代码部分
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
chap2.php:
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
$tpl->assign('me',array('xcf007','山东威海',26));
模板chap2.tpl(注意模板后缀任意,习惯用tpl你也可以用html等):
我的名字是
<!--{$me[0]}-->,我来自<!--{$me[1]}-->,本人年龄<!--{$me[2]}-->. ![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
$tpl->assign('me',array('name'=>'xcf007','city'=>'山东威海','age'=>26));
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
我的名字是<!--{$me.name}-->,我来自<!--{$me.city}-->,本人年龄<!--{$me.age}-->.
注意关联数组的引用方式,用的点.符号。
对于嵌套的数组道理一样,类似
$me.name.firstname这个样子。
3.)对象
chap2.php,简单的一段代码,属性公开:
class Person
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
{
public $name;
public $city;
public $age;
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
function __construct($name,$city,$age)
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
{
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
$
this->name=$name;
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
$
this->city=$city;
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
$
this->age=$age;
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
}
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
}
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
$me=
new Person('xcf007','山东威海',26);
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
$tpl->assign('me',$me);
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
我的名字是<!--{$me->name}-->,我来自<!--{$me->city}-->,本人年龄<!--{$me->age}-->.
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
chap2.php:
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
<?php
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
require_once(
"inc/smarty.inc.php");
//引入smarty
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
$tpl->assign('title',
"读取配置文件变量");
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
$tpl->display('chap2.tpl');
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
?>
在configs文件夹下建立menu.conf文件:
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
[chinese]
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
home =
"首页" ![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
introduction =
"公司介绍" ![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
contact =
"联系我们"
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
[english]
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
home =
"Home" ![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
introduction =
"Introduction" ![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
contact =
"Contact Us" ![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
<!--{config_load file=
"menu.conf" section=
"chinese"}-->
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
<html>
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
<head><title><!--{$title}--></title></head>
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
<body>
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
<p><a href=
"#"><!--{#home#}--></a> | <a href=
"#"><!--{$smarty.config.introduction}--></a> | <a href=
"#"><!--{#contact#}--></a></p>
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
</body>
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif)
</html>
通过config_load的smarty模板函数加载配置文件menu.conf,里面的chinese部分(就是中文菜单啦)
如果加载英文的可以
,<!--{config_load file="menu.conf" section="english"}--> 模板变量引用方式可以用#变量名字#方式,也可以$smarty保留变量的方式就是$smarty.config.变量名字的方式,各有特色。
##方式简单些,但有时出在引号里面时,就得考虑用保留变量方式了。
本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/157705 ,如需转载请自行联系原作者