/**
 * 编译模板文件,并且返回模板文件的路径
 * @param $file
 * @return string
 */
function renderTemplate($module, $action = null)
{
    if (defined('TEMPLATE_EXT')) {
        $ext = TEMPLATE_EXT;
    } else {
        $ext = ".htm";
    }
    if (empty($action)) {
        $file = "{$module}";
    } else {
        $file = "{$module}/{$action}";
    }
    $tplfile = TEMPLATE_DIR . '/' . $file . $ext;
    if (!file_exists($tplfile)) {
        die("template file {$tplfile} not exists");
    }
    $objfile = TEMPLATE_DATA_DIR . '/' . $file . '.tpl.php';
    if (!file_exists($objfile) || filemtime($tplfile) > filemtime($objfile)) {
        if (!file_exists(TEMPLATE_DATA_DIR)) {
            mkdir(TEMPLATE_DATA_DIR, 0777, true);
        }
        if (!file_exists(dirname($objfile))) {
            mkdir(dirname($objfile), 0777, true);
        }
        $t = new Template();
        if ($t->complie($tplfile, $objfile) === false) {
            die('Cannot write to template cache.');
        }
    }
    return $objfile;
}
Example #2
0
/**
 * 编译模板文件,并且返回模板文件的路径
 * @param $file
 * @return string
 */
function renderTemplate($module, $action = null)
{
    $file = "{$module}";
    if (!empty($action)) {
        $file .= "/{$action}";
    }
    if (!empty($GLOBALS['TEMPLATE_FILE'])) {
        $tplfile = $GLOBALS['TEMPLATE_FILE'];
        if (!file_exists($tplfile)) {
            die('no tpl found : ' . $tplfile);
        }
        $file_name = pathinfo($tplfile, PATHINFO_FILENAME);
        $objfile = TEMPLATE_DATA_DIR . "/{$file_name}.tpl.php";
        unset($GLOBALS['TEMPLATE_FILE']);
    } else {
        if (defined('TEMPLATE_EXT')) {
            $ext = TEMPLATE_EXT;
        } else {
            $ext = ".htm";
        }
        $tplfile = TEMPLATE_DIR . '/' . $file . $ext;
        if (!file_exists($tplfile)) {
            return false;
        }
        $objfile = TEMPLATE_DATA_DIR . '/' . $file . '.tpl.php';
    }
    if (!file_exists($objfile) || filemtime($tplfile) > filemtime($objfile)) {
        if (!file_exists(TEMPLATE_DATA_DIR)) {
            mkdir(TEMPLATE_DATA_DIR, 0777, true);
        }
        if (!file_exists(dirname($objfile))) {
            mkdir(dirname($objfile), 0777, true);
        }
        $t = new Template();
        if ($t->complie($tplfile, $objfile) === false) {
            die('Cannot write to template cache.');
        }
    }
    return $objfile;
}