今天学习smarty时候遇到Smarty error: unable to read resource问题,在csdn上看到以前的一个帖子的解决方法,个人在此基础上找到了另外一个方法。
在此和大家分享一下
原帖地址:http://topic.csdn.net/u/20070610/12/25acccd0-0744-4b07-b864-b346a86ae6cf.html#r_achor

解决方案 »

  1.   

    原帖问题:
    Warning:   Smarty   error:   unable   to   read   resource:   "index.tpl "   in   D:\AppServ\www\comm\Smarty.class.php   on   line   1095 代码就是个入门的范例程序,代码如下 <?php 
    require_once   ( "../comm/Smarty.class.php "); $smarty   =   new   Smarty(); 
    $smarty-> template_dir   =   './templates/ '; 
    $smarty-> compile_dir   =   './templates_c/ '; 
    $smarty-> config_dir   =   './configs/ '; 
    $smarty-> cache_dir   =   './cache/ '; 
    $smarty-> caching   =   false; 
    $smarty-> assign( 'CheckName ',   array( 
    1001   =>   '语文 ', 
    1002   =>   '数学 ', 
    1003   =>   '外语 ')); 
    $smarty-> assign( 'IsChecked ',   1001); $smarty-> assign( 'RadioName ',   array( 
    1001   =>   '语文 ', 
    1002   =>   '数学 ', 
    1003   =>   '外语 ')); 
    $smarty-> assign( 'IsChecked ',   1001); $smarty-> display( "index.tpl "); 
    ?>   
     
      

  2.   

    其中第10楼beyondzzz的回答对我很有启发,TA的答案如下:你的路径名有问题,我也遇到跟你一摸一样的问题。把下面的这段代码:
    require_once ( "../comm/Smarty.class.php ");  $smarty = new Smarty();  
    $smarty-> template_dir = './templates/ ';  
    $smarty-> compile_dir = './templates_c/ ';  
    $smarty-> config_dir = './configs/ ';  
    $smarty-> cache_dir = './cache/ ';  改写成:
    //这里:E:/AppServ/www/ 是我的控制台路径,就是在http上输入localhost之后出现的位置。
    define('BASE_PATH', 'E:/AppServ/www/');
    //这里:Smarty/是 我的Smarty的安装位置,也就是
    define(‘SMARTY_PATH’,‘Smarty/’);//这里是Smarty.class.php的路径:
    //合起来也就是:E:/AppServ/www/Smarty/Smarty.class.php
    require BASE_PATH.SMARTY_PATH.‘Smarty.class.php‘;
    $smarty=new Smarty();//下面的路径跟上面的差不多:
    $smarty->template_dir=BASE_PATH.SMARTY_PATH.‘templates/’;
    $smarty->complie_dir=BASE_PATH.SMARTY_PATH.’templates_c/‘;
    $smarty->config_dir=BASE_PATH.SMARTY_PATH.‘configs/’;
    $smarty->cache_dir=BASE_PATH.SMARTY_PATH.’cache/‘;总之你的路径一定要设置对了~就行~~
      

  3.   

    源代码:
    require_once   ( "../comm/Smarty.class.php ");$smarty   =   new   Smarty();
    $smarty-> template_dir   =   './templates/ ';
    $smarty-> compile_dir   =   './templates_c/ ';
    $smarty-> config_dir   =   './configs/ ';
    $smarty-> cache_dir   =   './cache/ ';
    $smarty-> caching   =   false;代码分析:
    如果没猜错的话,comm文件夹就是放smarty库文件的,一般是放到项目的根文件下,至少也得与templates和templates_c文件夹是同一级子文件夹吧。(如果项目不是很奇葩的话)
    require_once   ( "../comm/Smarty.class.php "); 以后的查找路径都会从../comm/下面开始,
    而$smarty-> template_dir   =   './templates/ ';此时文件会从当前目录../comm/下面开始寻找,(也就是smarty认为templates的完整路径是../comm/templates/)而一般templates文件夹是放到项目根目录下的,至少也是与smarty的库文件是同一级文件夹,不可能是是库文件comm的子文件夹,所以会找不到,因而提示不能读取的错误。
    正确的做法是用“..”让smarty再次回到上一层也就是comm文件夹的父目录去查找templates。这样就可以找到了。其他的路径雷同,修改如下:require_once   ( "../comm/Smarty.class.php ");$smarty   =   new   Smarty();
    $smarty-> template_dir   =   '../templates/ ';
    $smarty-> compile_dir   =   '../templates_c/ ';
    $smarty-> config_dir   =   '../configs/ ';
    $smarty-> cache_dir   =   '../cache/ ';
    $smarty-> caching   =   false;
      

  4.   

    我的环境是
    IDE:zendPHP9.0.1
    php5.3
    smarty2.6.18
    上面两种方法亲测可行