dousa1630 2011-08-05 09:44
浏览 36

php模板生成器无法获取数据

I have a problem with a templategenerator function that can't fetch some dynamic data and replace markers in a HTML with the data.

I have created a function inside my Main class that sets markers that I define

function GetMarkers($markers = array()) {

        foreach($markers as $key => $value) {
            $this->markers[strtoupper($key)] = $value;
        }

        return $this->markers;
    }

I define some markers in class.rooms.php

    $markersRoom = array();

    $markersRoom["###ROOMS###"] = '<a href="#" onclick="return showRBox();" class="boxOpener">'. $rTxt .'</a>';
    $markersRoom["###ROOMS###"] .= '<div class="roomBox">';
    $markersRoom["###ROOMS###"] .= '<ul class="roomItems">';

    while($row = mysql_fetch_array($sql)) {

        $markersRoom["###ROOMS###"] .= '<li>';
            $markersRoom["###ROOMS###"] .= '<p><a href="index.php?page=sr&room='. $row["room"] .'&floor='. $_GET["floor"] .'&wall='. $_GET["wall"] .'&envi='. $_GET["envi"] .'&fpanel='. $_GET["fpanel"] .'">'. $row["descr"] .'</a></p>';
        $markersRoom["###ROOMS###"] .= '</li>';
    }

    $markersRoom["###ROOMS###"] .= '</ul>';
    $markersRoom["###ROOMS###"] .= '</div>';

    Main::GetMarkers($markersRoom);
}

and puts them into the GetMarkers function.

These markers are to be used in a HTML template that are read by my TemplateGenerator function

function TemplateGenerator($template) {

    /* Get content from the html template */    
    $data = file_get_contents($template);

    if(isset($this->markers)) { 

        /**************************************************
        * Match each key in $this->markers array 
        * and replace with the correct value
        ***************************************************/ 
        foreach($this->$markers as $key => $value) {

            if(preg_match("/". preg_quote($key) ."/", $data, $matches)) {
                $data = str_replace($key, $value, $data);
            } else { 
                $data = $data;
            }
        }

        Main::TxtOutput($data);

    } else { 
        if(!empty($data)) { 
            echo $data;
        } else { 
            die("An error occured");
        }
    }
}

When I do a print_r($markers) inside GetMarkers() I can see all data from $markersRoom["###ROOMS###"] correctly but when I do it inside TemplateGenerator, the data is lost.

TemplateGenerator is called in my index.php inside a switch($page) with

$main->TemplateGenerator("templates/rooms.html");

Where rooms.html contain the marker ###ROOOMS###

It reads the HTML file correctly but doesn't have any data from GetMarkers to put into it so it just outputs ###ROOMS###

Am I missing something somewhere?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 potsgresql15备份问题
    • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
    • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
    • ¥60 pb数据库修改与连接
    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上