Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 模板机中的文本问题_Php_Templates_Text - Fatal编程技术网

Php 模板机中的文本问题

Php 模板机中的文本问题,php,templates,text,Php,Templates,Text,我有以下代码: class template { public $template,$prefix,$replace; function __construct($template, $prefix) { $this->prefix = $prefix; $this->template = $template; } public function SetValue($name, $replace) {

我有以下代码:

class template
{
    public $template,$prefix,$replace;
    function __construct($template, $prefix)
    {
        $this->prefix = $prefix;
        $this->template = $template;
    }
    public function SetValue($name, $replace)
    {
        $this->replace["#".$name."#"] = $replace;
    }
    public function Tempclude($found) 
    { 
        $file = "styles/main/".$found[1].'.html';

        if(!file_exists($file))     
            exit("the template '" . $found[1] . "' wasn't found."); 
        return file_get_contents($file); 
    }
    public function finish($stream = "normal")
    {
        if($stream == "folder")
            $code = file_get_contents("../styles/main/".$this->template.".html");
        else 
            $code = file_get_contents("styles/main/".$this->template.".html");

        $code = preg_replace_callback("@<pb:include=\"(.*)\">@iuU", array($this, 'Tempclude'), $code);

        if(is_array($this->replace))
            $code = str_replace(array_keys($this->replace), $this->replace, $code);

        $code = str_replace("\r\n", "", $code);
        $code = str_replace("   ", "", $code);
        echo $code;
    }
}
类模板
{
公共$template、$prefix、$replace;
函数构造($template,$prefix)
{
$this->prefix=$prefix;
$this->template=$template;
}
公共函数SetValue($name,$replace)
{
$this->replace[“#”。$name。”#“]=$replace;
}
公共函数Tempclude($found)
{ 
$file=“style/main/”$found[1]..html';
如果(!file_存在($file))
退出(“模板“.$found[1]”。“未找到”);
返回文件\u获取内容($file);
}
公共功能完成($stream=“normal”)
{
如果($stream==“folder”)
$code=file\u get\u contents(“../styles/main/”$this->template。“.html”);
其他的
$code=file\u get\u contents(“style/main/”$this->template。“.html”);
$code=preg_replace_回调(@@iuU),数组($this,'Tempclude'),$code);
如果(是_数组($this->replace))
$code=str\u replace(数组键($this->replace),$this->replace,$code);
$code=str\u replace(“\r\n”,”,$code);
$code=str_replace(“,”,$code);
echo$代码;
}
}
如果我在模板中键入一些简单的文本,页面将变为白色,内部没有任何内容

有什么问题吗?

没有问题。 尝试这样做:

$template = new template("index", ""); 

$template->SetValue("notice", "notice:"); 

$template->finish(); 
和index.html:

<pb:include="header"> 
    hhh 
    #Slider# 
<pb:include="footer"> 

啊
#滑块#
它的工作。 如果将index.html更改为:

<pb:include="header"> 
    <div id="hh"> 
        hhh 
    </div> 
    #Slider# 

啊
#滑块#

它返回一个白名单,里面有一些东西

  • 我发现未加引号的常量显示警告(取决于php配置,WAMP会)

  • 在您遇到错误的已编辑代码中,仅引用开头的两个常量。它应该可以正常工作。请按如下方式进行测试:

    $template=新模板(“索引”)
    $template->SetValue(“通知”,“通知:”)
    $template->finish()

  • 文件夹结构:必须提供styles/main。这些文件必须在main中找到:index.html、footer.html、header.html必须是私有的

  • 当一切正常时,请尝试删除任何不必要的评论或答案。
    谢谢大家!

    尝试添加错误报告(E_ALL);ini设置(“显示错误”,1);在开始时检查您的代码,并检查错误是什么。我想有一个致命的错误,你已经关闭了错误报告。干杯;)尝试重命名你的类,我认为类的名称不能是“模板”,这个词可能是保留的。。。另一件事是,您的脚本中是否包含了这个类所需的文件?语法也可能有问题“语法错误:语法错误,意外…”不,我输了;我建议您在php.ini文件中设置错误报告和显示错误-如果错误发生在脚本级别,它将无法“看到”您在那里调整它们的尝试。我不想在这里成为聚会的泡泡客,但为什么不尝试现有的模板引擎,如Twig?