如何向PHP包含文件添加内容?

时间:2013-12-14 23:31:53

标签: php html web include

好的我可能没有最好的头衔,但我会尽力给出更好的解释。

假设您使用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>

5 个答案:

答案 0 :(得分:4)

是的,你可以这样做:

的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) ?>

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

答案 1 :(得分:3)

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'); ?>

答案 2 :(得分:1)

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

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

答案 3 :(得分:1)

你可以这样做:

// 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";
?>

您可以看到为什么要使用第二个选项。

答案 4 :(得分:1)

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

<强>的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'); ?>