smarty教程系列2-section2

简介:
我们接着上次的内容讲解,首先这次把smarty定界符改了:
$tpl->left_delimiter = "<!-- {";//左定界符 
$tpl->right_delimiter = "}
-->";//右定界符
2.变量
下面是通过php赋值(assign)方式分配值:
1.)简单变量
通过 $smarty->assign('name','xcf007');
分配的变量,在模板里面我们可以像php中那样调用
<!--{$name}-->
2.)数组
这是简单的变量形式,下面看看数组的:
关键代码部分
InBlock.gifchap2.php: 
InBlock.gif$tpl->assign('me',array('xcf007','山东威海',26));
模板chap2.tpl(注意模板后缀任意,习惯用tpl你也可以用html等):
我的名字是 <!--{$me[0]}-->,我来自<!--{$me[1]}-->,本人年龄<!--{$me[2]}-->.
再看看关联数组的:
php部分:
InBlock.gif$tpl->assign('me',array('name'=>'xcf007','city'=>'山东威海','age'=>26));
模板部分:
InBlock.gif我的名字是<!--{$me.name}-->,我来自<!--{$me.city}-->,本人年龄<!--{$me.age}-->.
注意关联数组的引用方式,用的点.符号。
对于嵌套的数组道理一样,类似 $me.name.firstname这个样子。
3.)对象
chap2.php,简单的一段代码,属性公开:
InBlock.gif class Person 
InBlock.gif
InBlock.gif  public $name; 
InBlock.gif  public $city; 
InBlock.gif  public $age; 
InBlock.gif    
InBlock.gif function __construct($name,$city,$age) 
InBlock.gif { 
InBlock.gif    $ this->name=$name; 
InBlock.gif    $ this->city=$city; 
InBlock.gif    $ this->age=$age; 
InBlock.gif } 
InBlock.gif
InBlock.gif 
InBlock.gif$me= new Person('xcf007','山东威海',26); 
InBlock.gif$tpl->assign('me',$me); 
InBlock.gif 

模板chap2.tpl:
InBlock.gif我的名字是<!--{$me->name}-->,我来自<!--{$me->city}-->,本人年龄<!--{$me->age}-->.
注意对象访问操作符,这里是->
下面我们看看如何从配置文件获取值:
InBlock.gifchap2.php: 
InBlock.gif 
InBlock.gif<?php 
InBlock.gifrequire_once( "inc/smarty.inc.php"); //引入smarty 
InBlock.gif 
InBlock.gif$tpl->assign('title', "读取配置文件变量"); 
InBlock.gif 
InBlock.gif$tpl->display('chap2.tpl'); 
InBlock.gif 
InBlock.gif?> 
在configs文件夹下建立menu.conf文件:
InBlock.gif[chinese] 
InBlock.gifhome =  "首页" 
InBlock.gifintroduction =  "公司介绍" 
InBlock.gifcontact =  "联系我们" 
InBlock.gif 
InBlock.gif[english] 
InBlock.gifhome =  "Home" 
InBlock.gifintroduction =  "Introduction" 
InBlock.gifcontact =  "Contact Us" 
模板文件chap2.tpl:
InBlock.gif<!--{config_load file= "menu.conf" section= "chinese"}--> 
InBlock.gif<html> 
InBlock.gif<head><title><!--{$title}--></title></head> 
InBlock.gif<body> 
InBlock.gif <p><a href= "#"><!--{#home#}--></a> | <a href= "#"><!--{$smarty.config.introduction}--></a> | <a href= "#"><!--{#contact#}--></a></p> 
InBlock.gif</body> 
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 ,如需转载请自行联系原作者

相关文章
|
9月前
|
PHP
php模版引擎smarty出现Undefined index的解决方案
php模版引擎smarty出现Undefined index的解决方案
66 0
|
9月前
|
PHP 索引
php模版引擎smarty使用section方法实现for循环代用索引数字i的解决方案
php模版引擎smarty使用section方法实现for循环代用索引数字i的解决方案
59 0
|
PHP 索引
smarty手册-smarty中foreach循环语句详解
原文地址:smarty手册-smarty中foreach循环语句详解作者:谭博 {foreach}循环也有自身属性的变量,可以通过{$smarty.foreach.name.property}访问,其中"name"是name属性。
1185 0
|
Web App开发 PHP 索引
smarty、smarty格式化、smarty整数、smarty float、smarty各种转换方式、smarty日期转换等等
smarty code: {config_load file='config.conf'} Smarty学习 {capture name=banner}{*注释掉中间显示内容*} {include file="banner.
1235 0
|
PHP 前端开发 网络架构
|
PHP 搜索推荐 数据库