我是PHP的新手,所以我的理解可能完全不合适,但我正试着看看如何解决以下问题:
的template.php:
<html>
<head>
<title>Some title</title>
// Add a PHP 'Placeholder' here to inject some HTML if $filename = BodyInstance.php
</head>
<body>
<?php include($filename); // $filename = BodyInstance.php or can be any other php file ?>
</body>
</html>
BodyInstance.php
<p>
This is a dummy body text
<?php // Inject something into the placeholder of template.php ?>
</p>
所以,我有一个template.php文件,它根据$ filename参数将不同的视图加载到正文中。现在,其中一个视图,BodyInstance.php需要在head元素中有一些额外的标签。这需要在服务器端进行,我不想在document.ready上通过客户端的jQuery来实现。
任何提示?
由于
答案 0 :(得分:1)
我认为几乎没有选择。但通常是使用ob_*
函数制作的:
[index.php]
ob_start(); // start Output Buffer
require "content.php"; // Will create $headerContent and returns some html.
$content = ob_get_contents(); // Get content from buffer
ob_end_clean(); // Clear buffer
require "baseHtml.php";
[content.php]
<?php $headerContent = '<style>body {background-color: red;}</style>'; ?>
<p>Some text here</p>
[baseHtml.php]
<html>
<head>
<?php echo $headerContent; ?>
</head>
<body>
<?php echo $content; ?>
</body>
</html>
最终输出将是:
<html>
<head>
<style>body {background-color: red;}</style>
</head>
<body>
<p>Some text here</p>
</body>
</html>
这种技术通常用于CMS,因为它允许在某些内容被输出之后发送标题&#34; (仅用于缓冲)并允许预处理内容。
答案 1 :(得分:0)
我最终使用基于文件的加载模式根据$ filename-tags.php文件的存在加载标记
<html>
<head>
<title>Some title</title>
// Add a PHP 'Placeholder' here to inject some HTML if $filename = BodyInstance.php
<?php if (file_exists($filename ."-tags") include ($filename) ?>
</head>
<body>
<?php include($filename); // $filename = BodyInstance.php or can be any other php file ?>
</body>
</html>