如何将内容添加到PHP包含文件中


How to add content to PHP include files?

好吧,我可能没有最好的标题,但我会尽力给出更好的解释。

假设您正在使用PHP include()来构建您的网站:

Header.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name=keywords content="somthiefn"/>
        <title>Website</title>
        <link rel="stylesheet" href="css/style.css" type="text/css">
        <script src="Scripts/jquery-1.8.3.min.js"></script>
        <link rel="icon" type="image/png" href="/images/favicon.ico" />
    </head>
    <body>

Footer.php

    </body>
</html>

然后是一个示例页面:

Index.php

<!--Header-->
<?php include ('includes/header.php'); ?>
   <div class="content">
       <!-- some content-->
   </div>
<!--Footer-->
<?php include ('includes/footer.php'); ?>

基本上,我只是想知道是否有办法将一些脚本加载到我的头部分。

我想实现类似ASP的东西。NET及其母版页,在那里我可以直接添加内容的页眉。例如

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script src="Scripts/somescript.js"></script>
</asp:Content>

是的,你可以这样做:

index.php

<?php
    // Wanted page title 
    $title = "some";
    // JS Files.. 
    $scripts = array();  
    $scripts[] = '<script src="js/some.js" />';
    $scripts[] = '<script src="js/some2.js" />';
    $scripts[] = '<script src="js/some3.js" />';
    // Include header 
    include ('includes/header.php');

header.php

<title><?php echo $title ?></title>
<?php echo implode("'n",$scripts) ?>

当然,变量可以根据需要命名,并包含任何数据。最重要的是,你可以像我展示的那样在文件之间传递它们。

index.php中,在包含header.php之前,可以为脚本设置一个数组,如:

header.php

<?php if(!isset($scripts)) $scripts = array(); ?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name=keywords content="somthiefn"/>
        <title>Website</title>
        <link rel="stylesheet" href="css/style.css" type="text/css">
        <script src="Scripts/jquery-1.8.3.min.js"></script>
        <link rel="icon" type="image/png" href="/images/favicon.ico" />
        <!-- Include dynamic scripts-->
        <?php foreach($scripts in $script): ?>
        <script src="<?php echo $script; ?>"></script>
        <?php endforeach;?>
    </head>
    <body>

index.php

<?php
$scripts = array('Scripts/somescript.js', 'http://server.com/path/anoterscript.js);
?>
<!--Header-->
<?php include ('includes/header.php'); ?>
   <div class="content">
       <!-- some content-->
   </div>
<!--Footer-->
<?php include ('includes/footer.php'); ?>

是否要将PHP代码添加到标头中?然后,您可以在标记之间添加代码(如果启用了短标记,则可以添加标记),但我会考虑在PHP标记之间调用一个include,并在其中包含逻辑。

另一种选择可以是模板引擎,例如Smarty。在大多数模板引擎中,您可以定义自己的函数并从模板中调用它们。

您可以这样做:

// content.php
<?php
$head = "<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv='content-type' content='text/html;charset=utf-8' />
    <meta name='keywords' content='somthiefn' />
    <title>Website</title>
    <link type='text/css' rel='stylesheet' href='css/style.css' />
    <script src='Scripts/jquery-1.8.3.min.js'></script>
    <link type='image/png' rel='icon' href='images/favicon.ico' />
  </head>
<body>";
$foot = "'n<body>'n</html>";
?>
// index.php
<?php
include 'includes/content.php';
$dom = new DOMDocument; @$dom->loadHTML($head);
$hd = $dom->getElementsByTagName('head'); $hd = $hd->item(0);
$script = $dom->creatElement('script');
$scriptAttr = $dom->createAttribute('src');
$scriptAttr->value= 'Scripts/somescript.js'; $script->appendChild($scriptAttr);
$hd->appendChild($script);
echo $dom->saveHTML().$foot;
?>

另一种选择是使用变量来分隔代码,然后只分隔echo部分。这就是我想做的。它在技术上更快。试试这个:

// content.php
<?php
$headTop = "<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv='content-type' content='text/html;charset=utf-8' />
    <meta name='keywords' content='somthiefn' />
    <title>Website</title>
    <link type='text/css' rel='stylesheet' href='css/style.css' />
    <script src='Scripts/jquery-1.8.3.min.js'></script>'n    ";
$headBottom = "'n    <link type='image/png' rel='icon' href='images/favicon.ico' />
  </head>
<body>";
$foot = "'n<body>'n</html>";
?>
// index.php
<?php
include 'includes/content.php';
echo "$headTop<script src='Scripts/somescript.js'></script>$headBottom$foot";
?>

您可以理解为什么要使用第二个选项。

这对我很有用。它动态加载标题、脚本和样式。

header.php

<?php
    if(!isset($scripts))
        $scripts = array();
    if(!isset($styles))
        $styles = array();
?>
<!DOCTYPE html>
<html>
    <head>
        <title><?php echo $title ?></title>
        <link rel="stylesheet" href="css/somestyle.css" type="text/css">
        <script src=somescript.js"></script>
        <!-- include styles -->
        <?php foreach($styles as $style): ?>
            <link href="<?php echo $style; ?>" rel="stylesheet">
        <?php endforeach; ?>
        <!-- include scripts-->
        <?php foreach($scripts as $script): ?>
            <script src="<?php echo $script; ?>"></script>
        <?php endforeach;?>
    </head>
    <body>

index.php

<?php
    $title = "some dynamic title";
    $scripts = array('js/somescript.js', 'http://server.com/anoterscript.js');
    $styles = array('css/somestyle.css', 'css/anotherstyle.css');
    require_once ('header.php');
?>
   <div class="content">
       <!-- some content-->
   </div>
<?php require_once('footer.php'); ?>