我想要的是一个带有模板功能的php,例如
的template.php
function htmlTemplate($title){
echo'
<!DOCTYPE html>
<html>
<title>'.$title.'</title>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>';
}
我想要做的是有一个文件,用自己的名称分配$ title调用函数htmlTemplate($ title)
我想做的是让多个文档调用此函数并相应地更新标题:
使用example.php
<!DOCTYPE html>
<html>
<title>example</title>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
然后我可以根据需要重命名文件并更改标题以及使用DOM和其他php函数调用添加其他内容
答案 0 :(得分:2)
如果你想使用php你的功能是:
function htmlTemplate(){
echo'
<!DOCTYPE html>
<html>
<title>'. basename($_SERVER['PHP_SELF'], '.php') .'</title>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>';
}
答案 1 :(得分:1)
你的意思是:
<h1><?php echo str_replace( array('/', '.php'), $_SERVER['REQUEST_URI']); ?></h1>
答案 2 :(得分:1)
您可以将basename
函数与__FILE__
常量一起使用,如下所示:
$title = basename(__FILE__, '.php');
查看this answer以获取更多信息,并点击以下链接:
修改强>
以下是如何使用它的想法:
include('Template.php');
$currTitle = basename(__FILE__, '.php');
htmlTemplate($currTitle);
类似的东西。