刚学 Smarty 遇到了文件加载的问题。下面是我做的网站文件的结构01是整个工程的名称,总目录以及相关的目录如下01--+--
    |
    +SmartyLibs // smarty的配置文件
    |
    +subitem    // 一些加载 htm 的 php 程序
    |
    +templates  // 被加载 htm 文件
    我在subitem下面有个 informatics.php,这个要用 Smarty去加载一个 informatics.htm,这个文件位于templates下面。但是一加载就有问题了,下面是代码。
<?
         ......
$smarty->display("..\informatics.htm");  //这行有问题了
         ......
?>下面是 smarty 的配置文件<?
include_once("SmartyLibs/Smarty.class.php"); //包含smarty类文件
$smarty = new Smarty(); //建立smarty实例对象$smarty
$smarty->config_dir="Smarty/Config_File.class.php";  // 目录变量
$smarty->caching=false; //是否使用缓存,项目在调试期间,不建议启用缓存
$smarty->template_dir = "./templates"; //设置模板目录
$smarty->compile_dir = "./templates_c"; //设置编译目录
$smarty->cache_dir = "./smarty_cache"; //缓存文件夹
         $smarty->left_delimiter = "{";
$smarty->right_delimiter = "}";
?>

解决方案 »

  1.   


    编译目录?你指的是 templates 这个目录下面吗?
      

  2.   

     $smarty->template_dir = "./templates"; //设置模板目录
    你上面这一句已经设置路径了
    $smarty->display("informatics.htm");  //加载的时候就直接写文件名就可以
      

  3.   

    模板文件放在templates文件夹里
      不论你从哪调用,都相当于调用文件存放在templates文件夹里。
    当然,最后运行的还是原文件的路径
      

  4.   


    就是这么写的,但是感觉还是不对,我的文件放在 01/subitem下面,跟我的php防止有关系么?
    系统报错为:
    Warning: Smarty error: unable to read resource: "informatics.htm" in C:\Www_local\01\SmartyLibs\Smarty.class.php on line 1095<?phpinclude_once("../smarty_inc.php");
    include_once("../conn.php"); $pagecurrent = 0;
    $pagetotal;
    $pagesize = 10; if($_POST['submit']){
    $user    = $_POST['avatarName'];
    $title   = $_POST['title'];
    $content = $_POST['content']; $sql="INSERT INTO chinese_informatics (id,avatarName,title,content,lastdate) " .
       "values ('','$user','$title','$content','now()')"; $db->query("set names 'GBK'");
    $rs = $db->query($sql); } $rs = $db->query("SELECT * FROM chinese_informatics");
    $pagetotal = $rs->rowcount();
    $c = 0;
    $rs = $db->query("SELECT * FROM chinese_informatics LIMIT $c, 5");
    echo $num; while( $row = $rs->fetch()){
    //print_r($row);
    $id  = $row[id];
    $avatarName = $row[avatarName];
    $title = $row[title];
    $content = $row[content];
    $lastdate = $row[lastdate];
    $MesContent[] = array("id"=>$id,"avatarName"=>$avatarNAme,"title"=>$title,"content"=>$content,"lastdate"=>$lastdate);
    }
    $smarty->assign("MesContent",$MesContent);
    $smarty->display("informatics.htm");?>
      

  5.   

    你有没有把informatics.htm这个文件放在templates里面?
      

  6.   

    应该是相对路径设置的问题,
    就当前文件引用include_once("../smarty_inc.php");
    是在你的上一级目录,而读取这个设置文件之后
    include_once("SmartyLibs/Smarty.class.php"); //包含smarty类文件
    它会给予当前文件的目录来查找rtyLibs/Smarty.class.php,显然是没有的。我这么理解的
    我用过一次smarty出现了这样的问题,我当时也是这么思路解决的,在当前文件夹下新建一个smarty配置文件,内容为include_once("../smarty/Smarty.class.php"); //包含smarty类文件
    $smarty = new Smarty(); //建立smarty实例对象$smarty
    $smarty->config_dir="smarty/Config_File.class.php";  // 目录变量
    $smarty->caching=false; //是否使用缓存,项目在调试期间,不建议启用缓存
    $smarty->template_dir = "../templates"; //设置模板目录
    $smarty->compile_dir = "../templates_c"; //设置编译目录
    $smarty->cache_dir = "../smarty_cache"; //缓存文件夹$smarty->left_delimiter = "{";
    $smarty->right_delimiter = "}";