一般出现这种错误是smarty配置的问题,路径没有配置好!如下面这段代码是将会出现这样的错误:
Warning: Smarty error: unable to read resource: “templates\smtl.html” inF:\wamp\www\shangage\smarty\Smarty.class.phpon line1095,无法读取资源,即无法读取smtl.html模版文件,
<?php
define(‘base_path’,$_SERVER['DOCUMENT_ROOT']);//定义服务器的绝对路径
define(‘smarty_path’,'\shangage\smarty\\’);//定义smarty目录的绝对路径
require base_path.smarty_path.’Smarty.class.php’;//加载Smarty类库文件
$smarty = new Smarty;//实例化一个smarty对象
//定义个目录的路径
$smarty->template_dir = base_path.smarty_path.’tmeplates\\’;
$smarty->compile_dir = base_path.smarty_path.’templates_c\\’;
$smarty->config_dir = base_path.smarty_path.’config\\’;
$smarty->cache_dir = base_path.smarty_path.’cache\\’;
$arr = array(1=>’杨’,2=>’君’,3=>’华’,4=>’杨君华’);
$smarty->assign(‘title’,'smarty的第一次使用’);
$smarty->assign(‘arr’,$arr);
$smarty->display(‘templates\smtl.html’)
?>
把templates\smtl.html改写成..\templates\smtl.html,就可以调用模版文件。输出结果为:
还可以把代码写成如下:
<?php
define(‘base_path’,$_SERVER['DOCUMENT_ROOT']);//定义服务器的绝对路径
define(‘smarty_path’,‘/shangage/smarty/’);//定义smarty目录的绝对路径
require base_path.smarty_path.’Smarty.class.php’;//加载Smarty类库文件
$smarty = new Smarty;//实例化一个smarty对象
//定义个目录的路径
$smarty->template_dir = base_path.smarty_path.‘tmeplates/’;
$smarty->compile_dir = base_path.smarty_path.‘templates_c/’;
$smarty->config_dir = base_path.smarty_path.‘config/’;
$smarty->cache_dir = base_path.smarty_path.‘cache/’;
$arr = array(1=>’杨’,2=>’君’,3=>’华’,4=>’杨君华’);
$smarty->assign(‘title’,'smarty的第一次使用’);
$smarty->assign(‘arr’,$arr);
$smarty->display(‘../templates/smtl.html’)
?>
注意:正斜杠 / 与反斜杠 \ 是一样的,但是要注意:反斜杠 \ 在PHP中为转义字符,没有意义。如要输出单引号 ‘ 则应该写成 \ ‘,因此 \\ ‘ 实际上就是 / ‘ 。注意,模版文件要与调用模版文件的字符集编码一样,否则也会出现上面的错误!