刚刚接触Smarty,有很多疑惑,代码:
<?php
require("./libs/Smarty.class.php");
$smarty = new Smarty();
$smarty->templatfe_dir='./templates';
$smarty->compile_dir='./templates_c';
$smarty->config_dir='./configs';
$smarty->cache_dir='./cache';
//$smarty->caching=true;
//$smarty->cache_lifetime=300;
//模板边界符设定,主要是防止标签冲突
$smarty->left_delimiter='{';
$smarty->right_delimiter='}';
?>
对于上面的很多变量,比如 $smarty->left_delimiter$smarty->right_delimiter应该是在整个web中都使用一样的,没有谁去更改这些变量让代码更加复杂,我觉得这些变量应该设置成static变量,或者Smarty类完全有理由设置成单例的,不要每次去都去new一个对象,因为里面很多属性都只需要初始化一次就可以了。
小弟刚接触Smarty,对下面还没有深入的学习,请大牛指点出我的错误.
<?php
require("./libs/Smarty.class.php");
$smarty = new Smarty();
$smarty->templatfe_dir='./templates';
$smarty->compile_dir='./templates_c';
$smarty->config_dir='./configs';
$smarty->cache_dir='./cache';
//$smarty->caching=true;
//$smarty->cache_lifetime=300;
//模板边界符设定,主要是防止标签冲突
$smarty->left_delimiter='{';
$smarty->right_delimiter='}';
?>
对于上面的很多变量,比如 $smarty->left_delimiter$smarty->right_delimiter应该是在整个web中都使用一样的,没有谁去更改这些变量让代码更加复杂,我觉得这些变量应该设置成static变量,或者Smarty类完全有理由设置成单例的,不要每次去都去new一个对象,因为里面很多属性都只需要初始化一次就可以了。
小弟刚接触Smarty,对下面还没有深入的学习,请大牛指点出我的错误.
是吗?模板文件永远都是明文的,我完全有可能不想让我的模板被你直接使用“不要每次去都去new一个对象”
任何使用 smarty 的项目不都只实例化一个 smarty 对象吗?
能举出实例化多个的例子吗?
php里类的功能也很不完善, 比如你说的单例模式在php4的就不大方便完美实现...在那时的情况下,一般都是在一个公共文件里定义一下$smarty然后该文件被各个页面调用,
实际上也只new了一次另一方面,不写成单例也保留了可能会有2个实例的可能,设想一下如果一个项目是由2个存在的项目合并
而成,该2项目都用了smarty但却用了不同的设置....
如果你觉得难的话,我这边有个用php做模板的代码如下..不需要去学一门语言,而且效率高我自己写的,心中无剑的模板引擎..
<?php
class view{
private $_data=array();
public static $spath='';
function __construct(){
$this->path=self::$spath;
}
function assign($field,$data=null){
if(is_array($field)){
foreach($field as $key=>$value){
$this->assign($key,$value);
}
}else{
$this->_data[$field]=$data;
}
} function set_path($path){
$this->path=rtrim($path,'/');
self::$spath=$this->path;
} function clear_assign($field=''){
if($field){
unset($this->_data[$field]);
}else{
$this->_data=array();
}
} function fetch($tpl){
$tpl="{$this->path}/$tpl";
$c='';
if(!is_file($tpl)){
trigger_error("没有找到[$tpl]模板",E_USER_WARNING);
}else{
extract($this->_data);
ob_start();
include($tpl);
$c= ob_get_clean();
}
return $c;
} function display($tpl){
echo $this->fetch($tpl);
}
}
?>我的模板<LINK href="<?php echo $baseDir?>common/js/jq/themes/ui.datepicker.css" rel=stylesheet type=text/css>
<script type="text/javascript" src="<?php echo $baseDir?>common/js/jq/ui/ui.datepicker.js"></script>
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><?=$title?></td>
</tr>
<tr>
<td>
<p class="tongji_des">*点击表格中的蓝色数字可导出对应的详细用户数据</p>
<table cellspacing="0" cellpadding="0" width="100%" class="tongji_so"> <tr><td>
<form name="weblogin" method="post" action="?">
<input name="conditon[time]" class="time" type="text" value="<?=htmlspecialchars($_REQUEST[weblogin][conditon][time])?>" size="40" readonly = true id="time"/>
<select name="conditon[area_id]">
<?foreach($areas as $id=>$area):?>
<option value='<?=$id?>' <?if($_REQUEST[weblogin][conditon][area_id]==$id):?>selected<?endif;?>>
<?=$area?>
</option>
<?endforeach?>
</select>
<select>
<option value="weblogin">web登陆次数</option>
</select>
<input type="submit" name="filtersubmit" value="查询">
<input type="hidden" name="act" value="weblogin">
<input type="hidden" name="do" value="index">
</form>
</td></tr>
<tr><td>
<form name="sms" method="post" action="?">
<input name="conditon[time]" class="time" type="text" value="<?=htmlspecialchars($_REQUEST[sms][conditon][time])?>" size="40" readonly = true id="time"/>
<select name="conditon[area_id]">
<?foreach($areas as $id=>$area):?>
<option value='<?=$id?>' <?if($_REQUEST[sms][conditon][area_id]==$id):?>selected<?endif;?>>
<?=$area?>
</option>
<?endforeach?>
</select>
<select name="conditon[channel]">
<option value="sms" <?if($_REQUEST[sms][conditon][channel]=='sms'):?>selected<?endif;?>>web登陆短信发送次数统计</option>
<option value="smsup" <?if($_REQUEST[sms][conditon][channel]=='smsup'):?>selected<?endif;?>>通道号上行数量统计</option>
<option value="smsuphaoma" <?if($_REQUEST[sms][conditon][channel]=='smsuphaoma'):?>selected<?endif;?>>通道号上行号码数量统计</option>
</select>
<input type="hidden" name="act" value="<?if($_REQUEST[sms][conditon][channel]):?><?=$_REQUEST[sms][conditon][channel]?><?else:?>sms<?endif;?>">
<input type="hidden" name="do" value="index">
<input type="submit" name="filtersubmit" value="查询">
</form>
</td></tr> <tr><td>
<form name="huoyaodu" method="post" action="?">
<input name="conditon[time]" class="time" type="text" value="<?=htmlspecialchars($_REQUEST[huoyaodu][conditon][time])?>" size="40" readonly = true id="time"/>
<select name="conditon[area_id]">
<?foreach($areas as $id=>$area):?>
<option value='<?=$id?>' <?if($_REQUEST[huoyaodu][conditon][area_id]==$id):?>selected<?endif;?>>
<?=$area?>
</option>
<?endforeach?>
</select>
<label><input type="radio" name="radio" id="radio" value="radio" checked/>活跃用户数</label>
<input type="hidden" name="act" value="huoyaodu">
<input type="hidden" name="do" value="index">
<input type="submit" name="filtersubmit" value="查询">
</form>
</td></tr>
</table>
<?=$result?>
</td></tr></table>
<script language="JavaScript">
<!--
$(function(){
$("select[name='conditon[channel]']").change(function(){
var val=$(this).val();
var $form=$(this).parents("form:eq(0)");
$form.find("[name='act']").val(val);
});
});
//-->
</script>
<script type="text/javascript">
$(function(){
$(".time").datepicker({ // Default regional settings
rangeSelect: true, // Allows for selecting a date range on one date picker
rangeSeparator: ' | ', // Text between two dates in a range
clearText: '清除', // Display text for clear link
closeText: '关闭', // Display text for close link
prevText: '<前进', // Display text for previous month link
nextText: '后退>', // Display text for next month link
currentText: '今天', // Display text for current month link
monthNames: ['一月','二月','三月','四月','五月','六月',
'七月','八月','九月','十月','十一月','十二月'], // Names of months for drop-down and formatting
dateFormat: 'yymmdd', // Display text for clear link
onClose:function(dates,inst){
var val=$(this).val().split(" | ");
// if(val[0]&&val[1]&&((parseInt(val[1])-parseInt(val[0]))<=10)){//<10天的为合法的
// }else{
// alert("时间不合法或者范围过大");
// $(this).val('');//清空掉
// }
}
});
$("form").submit(function(){
var val=$(this).find("input[name='conditon[time]']").val();
if($.trim(val)==''){
alert("日期必须填写");
return false;
}
});
});
</script>
Smarty.inc.php:
<?php
require("./libs/Smarty.class.php");
$smarty=new Smarty();
$smarty->templatfe_dir='./templates';
$smarty->compile_dir='./templates_c';
$smarty->config_dir='./configs';
$smarty->cache_dir='./cache';
//$smarty->caching=true;
//$smarty->cache_lifetime=300;
//模板边界符设定,主要是防止标签冲突
$smarty->left_delimiter='{';
$smarty->right_delimiter='}';
?>
index.php:
<?php
require('smarty.inc.php');
$smarty->assign( 'person', $chico );
$smarty->display( 'template.tpl' );
?>
是不是就这样使用就可以了,在一个文件里面创建Smarty对象,然后在其他要使用smarty页面直接调用对象就可以了。感觉php真的很乱。咳
==================
你直接include不是更好?
所谓模板:个人觉得只要做到页面分离就可以了,其他的什么缓存之类的,不应该是它要做的,交由框架