在PHP中,从包含的PHP文件在基本PHP文件中调用/注入某些内容

在PHP中,从包含的PHP文件在基本PHP文件中调用/注入某些内容,php,Php,我是PHP新手,所以我的理解可能完全不正确,但我正在尝试了解如何解决以下问题: template.php: <html> <head> <title>Some title</title> // Add a PHP 'Placeholder' here to inject some HTML if $filename = BodyInstance.php </head> <bo

我是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>

一些头衔
//如果$filename=BodyInstance.PHP,在此处添加一个PHP“占位符”以注入一些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进行

有什么提示吗


谢谢

我想没有多少选择了。但通常它是使用函数制作的:

[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>
[index.php]
ob_start();//启动输出缓冲区
需要“content.php”;//将创建$headerContent并返回一些html。
$content=ob_get_contents();//从缓冲区获取内容
ob_end_clean();//清除缓冲区
需要“baseHtml.php”;
[content.php]
这里有一些文字

[baseHtml.php]
最终产出将是:

<html>
  <head>
     <style>body {background-color: red;}</style>
  </head>
  <body>
     <p>Some text here</p>
  </body>
</html>

正文{背景色:红色;}
这里有一些文字


这种技术通常在CMS中使用,因为它允许在某些内容“输出”(仅缓冲)后发送标题,并允许对内容进行预处理。

我最终使用基于文件的加载模式来加载基于$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>

一些头衔
//如果$filename=BodyInstance.PHP,在此处添加一个PHP“占位符”以注入一些HTML

这似乎是一个糟糕的想法——我想你应该重新思考一下你的想法,但是你可以通过
在那里包含一个文件,并对照
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu<代码>“$filename”
将查找实际名为“$filename”的文件。完全不使用引号,或者使用无意义的双引号。为什么不在另一个文件中添加这些额外的标记,并
将第二个文件包含在所需页面中…这肯定会让您头痛不已!:)@NoobEditor:我可以,但那会打破我的模式,从而打破一致性。php是一个通用的加载器类,它应该在加载的所有文件中保持一致。充其量(理想情况下),它应该提供一些占位符重写,这些占位符重写可以被加载的类重写。这就是我的想法,我可能是错的,这就是为什么在这里寻找输入:)@sppc42:在这种情况下,在加载之前,使用
get_class()
检测类,然后根据
$filename
参数相应地加载…意思是如果
get_class()
提供需要这些额外标记的类,并且
$filename
让它们…加载它…有意义吗?谢谢,这看起来很有用。我还考虑在template.php中使用include($filename.-tags)之类的东西,并仅在它存在时加载它。因此,BodyInstance.php可以定义另一个BodyInstance-tags.php,而其他派生类则不能。今天我们将尝试这两种方法,并重新发布最有效的方法。谢谢@sppc42是的,您一定要应用动态文件名,在使用它们之前还要检查文件/变量是否存在