PHP问题【急】【急】【急】【急】请大神帮忙

sysmaze 2014-11-02 03:42:43
模板赋值问题
最终页代码如下

<?php
require "system/system.php";
$tpl = new KSeeing();
$tpl->assign("show",'success');
$show = '111';
include('templates/index.html'); =======================这样写的话 $show能成功赋值
//include('F:/AppServ/www/temp/tpl/com_index.html.php');
//$tpl->compile('index.html');===================这样写的话$show不能赋值
?>

index.html 简要代码如下 输出一个变量

<div class="logo"><?php echo $show ?></div>


问题如下:
直接在本页面下include(上面第一个代码块) include('templates/index.html'); 成功赋值可以输出$show值
简单写了个模板 核心函数 $tpl->compile('index.html'); 如下 就不能成功赋值 相对绝对路径都试过,【以下代码compile已改为绝对】,而且两个引用的路径一模一样[非绝对下]竟然不好使!!!不知道是不是涉及到GLOBAL这个东西,请大神详解 ,在线等!


function compile($fileName){
$ducument_root = $_SERVER['DOCUMENT_ROOT'];
$tplFile = $ducument_root.$this->template_dir.$fileName; //找到模版文件
if(!file_exists($tplFile)){
return false;
}
$comFile = $ducument_root.$this->compile_dir.'com_'.basename($tplFile).'.php';//构造编译后的文件
if(!file_exists($comFile) || filemtime($comFile)<filemtime($tplFile)){
$repContent = $this->tpl_replace(file_get_contents($tplFile));//获取源文件内容并替换成php源格式
$handle = fopen($comFile,'w+');
fwrite($handle,$repContent);
fclose($handle);
}
include($comFile);
}


...全文
108 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2014-11-02
  • 打赏
  • 举报
回复
所以我要你给出 assign 方法的代码,这样才能有的放矢 你在 compile 中 include 不行的元婴是 compile 中并没有变量 $show
sysmaze 2014-11-02
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
你的 KSeeing::assign 方法是怎么写的? 一般的说 $tpl->assign("show",'success'); 是将 'success' 赋予 $tpl 的 show 属性,通常会有一个载体 所以可能是这样 $tpl->data['show'] = 'success'; 你的模板中是 <?php echo $show ?> 所以 compile 方法中应是
extract($this->data);
include($comFile);
=============================================== 2楼终极页面有个没注释 修改下

<?php 
    require "system/system.php"; 
    $tpl =  new KSeeing();
    $tpl->assign("show",'success');
    //$show = '111';
    //include('templates/index.html');
    //include('F:/AppServ/www/temp/tpl/com_index.html.php');
    $tpl->compile('index.html');
?>
sysmaze 2014-11-02
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
你的 KSeeing::assign 方法是怎么写的? 一般的说 $tpl->assign("show",'success'); 是将 'success' 赋予 $tpl 的 show 属性,通常会有一个载体 所以可能是这样 $tpl->data['show'] = 'success'; 你的模板中是 <?php echo $show ?> 所以 compile 方法中应是
extract($this->data);
include($comFile);
版主大人 compile这里面有个正则替换 我是故意在终极页面 写个变量$show = '111'; 看看能不能输出 正常的应该是$tpl->assign("show",'success'); function assign($tpl_var,$value = null){ if($tpl_var!=''){ $this->vars[$tpl_var] = $value; } } 现在的问题很纠结具体的情况是这样的 这是终极页面

<?php 
    require "system/system.php"; 
    $tpl =  new KSeeing();
    $tpl->assign("show",'success');
    //$show = '111';
    include('templates/index.html');
    //include('F:/AppServ/www/temp/tpl/com_index.html.php');
    $tpl->compile('index.html');
?>
目前的终极页面可以正常输出success 这是没问题的 然后我稍加修改

<?php 
    require "system/system.php"; 
    $show = '111';
   
     $tpl->compile('index.html');
?>
这样index.html里的$show就没值了,问题来了!!!如果我把$tpl->compile('index.html');改成 include('templates/index.html');就好使了!!而$tpl->compile('index.html');得到的结果也是include('templates/index.html');或者是绝对路径 这样为啥就不好使了呢 目前表面问题看着像页面问题
xuzuning 2014-11-02
  • 打赏
  • 举报
回复
你的 KSeeing::assign 方法是怎么写的? 一般的说 $tpl->assign("show",'success'); 是将 'success' 赋予 $tpl 的 show 属性,通常会有一个载体 所以可能是这样 $tpl->data['show'] = 'success'; 你的模板中是 <?php echo $show ?> 所以 compile 方法中应是
extract($this->data);
include($comFile);

21,881

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧