首頁 >php教程 >php手册 >PHP简易渲染模板

PHP简易渲染模板

WBOY
WBOY原創
2016-06-06 20:12:391079瀏覽

渲染模板 function render_template($template_file, $variables) { extract($variables, EXTR_SKIP); // Extract the variables to a local namespace ob_start(); // Start output buffering include "./$template_file"; // Include the template file $c

渲染模板

function render_template($template_file, $variables) {
    extract($variables, EXTR_SKIP);  // Extract the variables to a local namespace
    ob_start();                      // Start output buffering
    include "./$template_file";      // Include the template file
    $contents = ob_get_contents();   // Get the contents of the buffer
    ob_end_clean();                  // End buffering and discard
    return $contents;                // Return the contents
}

usage

$render_message = array(
    'token' => $token,
    'me' => $user,
    'game_key' => $game_key,
    'game_link' => $game_link,
    'initial_message' => $initial_message,
    );
$template_file = 'index.tpl';
$ret = render_template($template_file, $render_message);
echo($ret);
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn