将HTML模板与PHP代码混合:PHP HTML模板


mix html template with php code : php html template

我使用 php/mysql 创建了一个动态网站。 我的模板使用纯PHP代码+ html打印数据。 现在,更好,优化,更快的混合PHP + HTML结构的方法?(不带模板引擎)

对于我的每一页:Ex.1(包装器之前的PHP代码)

<?PHP include ( DEFINE_SITE . '/templates/header.php' );
// isset : post : get : SELECT : INSERT ... Or ANY CODE OF PHP
 ?>
<div id="wrapper">
<div id="leftsidebar"><?PHP // Left DATA ?></div>
<div id="centercontent">
<?PHP // while {} Print result of php  ANY LOOPS ..... ?>
</div>
<div id="rightsidebar"><?PHP // Right DATA ?></div>
</div>
<?PHP include ( DEFINE_SITE . '/templates/footer.php' ); ?>

对于每个页面,我都有:Ex.2(在侧边栏之后和中心内容之前)

<?PHP include ( DEFINE_SITE . '/templates/header.php' );
<div id="wrapper">
<div id="leftsidebar"><?PHP // Left DATA ?></div>
<?PHP // isset : post : get : SELECT : INSERT ... Or ANY CODE OF PHP ?>
<div id="centercontent">
<?PHP // while {} Print result of php  ANY LOOPS ..... ?>
</div>
<div id="rightsidebar"><?PHP // Right DATA ?></div>
</div>
<?PHP include ( DEFINE_SITE . '/templates/footer.php' );?>

哪个更好?e.x 1 或 e.x 2 ?你的意见 ?

为什么厌恶模板引擎? 实际上,PHP 是一个模板引擎,但也许如果你检查了一些(如 Twig),它可能会帮助你在没有模板引擎的情况下设计更灵活、更快速和响应更灵敏的东西...... 或者你可能会开始喜欢它,就像我一样;-)

我建议你把HTML和PHP分开。如果您不想使用模板系统,为什么不这样做呢?

制作一个 HTML 模板:

<div id="wrapper">
    <div id="leftsidebar">{LEFT}</div>
    <div id="centercontent">{CONTENT}</div>
    <div id="rightsidebar">{RIGHT}</div>
</div>

在不同的 php 文件中:

$content = file_get_contents('template.html');
$templateVars = array();
startVar('CONTENT');
echo '<p>This is the content.</p>'; // New template vars are also allowed
endVar('CONTENT');
echo replaceTemplateVars($content);
/**
 * Replace template variables recursively with the key-value pairs that are in the $templateVars queue
 * @param string $content (default NULL) is code to convert.
 * @return the string that is replaced
 */
function replaceTemplateVars($content = NULL)
{
    global $templateVars;
    $matches = array();
    preg_match_all("/{[A-Z0-9_:]*}/", $content, $matches); // $matches = All vars found in your template
    foreach($matches[0] as $key)
    {
        if(isset($templateVars[substr($key, 1, -1)]))
        {
            $content = str_replace($key, $templateVars[substr($key, 1, -1)], $content); // Substitute template var with contents
        }
        else
        {
            $content = str_replace($key, "", $content); // Remove empty template var from template
        }
    }
    // Check if the replacement has entered any new template variables, if so go recursive and replace these too
    if(preg_match_all("/{[A-Z0-9_:]*}/", $content, $matches))
    {
        $content = replaceTemplateVars($content);
    }
    return $content;
}
/**
 * Start output buffer for a template key
 * @param string $key is not used. Only for overview purposes
 */
function startVar($key)
{
    ob_start();
}
/**
 * End output buffer for a template key and store the contents of the output buffer in the template key
 * @param string $key
 */
function endVar($key)
{
    setVar($key, ob_get_contents());
    ob_end_clean();
}
/**
 * @param string $key to store value in
 * @param mixed $value to be stored
 */
function setVar($key, $value)
{
    global $templateVars;
    $templateVars[$key] = $value;
}

这就是您在屏幕上看到的内容:

<div id="wrapper">
    <div id="leftsidebar"></div>
    <div id="centercontent"><p>This is the content.</p></div>
    <div id="rightsidebar"></div>
</div>

因此,首先完成预处理,最后输出所有html代码。

当然,您可以添加 startPrependVar()startAppendVar() 等函数,并将所有内容包装到 Template 类中以摆脱全局范围。