欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2024-01-14 15:09:04
...
模板赋值问题
最终页代码如下
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 简要代码如下 输出一个变量
	


问题如下:
直接在本页面下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)tpl_replace(file_get_contents($tplFile));//获取源文件内容并替换成php源格式                $handle = fopen($comFile,'w+');                fwrite($handle,$repContent);                fclose($handle);            }            include($comFile);        }



回复讨论(解决方案)

你的 KSeeing::assign 方法是怎么写的?

一般的说
$tpl->assign("show",'success');
是将 'success' 赋予 $tpl 的 show 属性,通常会有一个载体
所以可能是这样 $tpl->data['show'] = 'success';

你的模板中是
所以 compile 方法中应是

extract($this->data);include($comFile);

你的 KSeeing::assign 方法是怎么写的?

一般的说
$tpl->assign("show",'success');
是将 'success' 赋予 $tpl 的 show 属性,通常会有一个载体
所以可能是这样 $tpl->data['show'] = 'success';

你的模板中是
所以 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;
}
}




现在的问题很纠结具体的情况是这样的

这是终极页面
assign("show",'success');    //$show = '111';    include('templates/index.html');    //include('F:/AppServ/www/temp/tpl/com_index.html.php');    $tpl->compile('index.html');?>


目前的终极页面可以正常输出success 这是没问题的 然后我稍加修改
compile('index.html');?>


这样index.html里的$show就没值了,问题来了!!!如果我把$tpl->compile('index.html');改成

include('templates/index.html');就好使了!!而$tpl->compile('index.html');得到的结果也是include('templates/index.html');或者是绝对路径 这样为啥就不好使了呢 目前表面问题看着像页面问题

你的 KSeeing::assign 方法是怎么写的?

一般的说
$tpl->assign("show",'success');
是将 'success' 赋予 $tpl 的 show 属性,通常会有一个载体
所以可能是这样 $tpl->data['show'] = 'success';

你的模板中是
所以 compile 方法中应是

extract($this->data);include($comFile);



===============================================
2楼终极页面有个没注释 修改下
assign("show",'success');    //$show = '111';    //include('templates/index.html');    //include('F:/AppServ/www/temp/tpl/com_index.html.php');    $tpl->compile('index.html');?>

所以我要你给出 assign 方法的代码,这样才能有的放矢
你在 compile 中 include 不行的元婴是 compile 中并没有变量 $show

上一篇: Symfony核心类概述

下一篇: ST表模板