如何向PHP包含文件添加内容? [英] How to add content to PHP include files?

查看:100
本文介绍了如何向PHP包含文件添加内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Ok I may not have the best title , but I will try to give a better explanation.

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

Let's assume you are using PHP include() to structure your website:

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>

然后显示一个示例页面:

Then a sample page:

Index.php

<!--Header-->
<?php include ('includes/header.php'); ?>

   <div class="content">
       <!-- some content-->
   </div>

<!--Footer-->
<?php include ('includes/footer.php'); ?>

基本上,我只是想知道是否可以将一些脚本加载到我的标头部分中.

Basically I just want to know if there is a way to load some script into my header section.

我想要实现类似ASP.NET及其母版Page的功能,在其中我可以仅在标题中添加内容.例如

I would like to achieve something like ASP.NET and its master Page, where I can just add content the header. for example

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

推荐答案

index.php 中,在包含 header.php 之前,可以为您的数组设置数组像这样的脚本:

In index.php, before you include header.php, you could set an array for your scripts like:

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包含文件添加内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆