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

php中文网
发布: 2016-06-23 13:46:22
原创
951人浏览过

模板赋值问题
最终页代码如下

<?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);        }
登录后复制


AI建筑知识问答
AI建筑知识问答

用人工智能ChatGPT帮你解答所有建筑问题

AI建筑知识问答 22
查看详情 AI建筑知识问答

回复讨论(解决方案)

你的 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;
            }
        }




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

这是终极页面
<?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');或者是绝对路径                        这样为啥就不好使了呢    目前表面问题看着像页面问题 

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

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

你的模板中是 
所以 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');?>
登录后复制

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

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号