【发布时间】:2018-10-07 15:24:09
【问题描述】:
我正在做一个小项目,我想应用某种模板来显示主模板文件之间的内容,当包含在每个页面上时,不使用任何模板引擎。目前我有以下页面。
layout.template.php
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<!-- page content displays here -->
<?php show_content(); ?>
</body>
</html>
index.php
<?php function show_content(){ ?>
<div>
<h1>Home page</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<?php }
include 'layout.template.php';
?>
second.php
<?php function show_content(){ ?>
<div>
<h1>Second page</h1>
<p>some other content</p>
</div>
<?php }
include 'layout.template.php';
?>
我使用 show_content() 函数来定义将呈现到每个页面上的 layout.template.php 文件中的 html。当 layout.template.php 包含它时,它会调用该函数并根据包含它的页面在其中显示 html 代码。我的问题是
这是一个好主意或做法吗? 我有什么替代方法来实现这一点(除了使用模板引擎)?
【问题讨论】:
标签: php html function templating