Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何配置页面以搜索外部布局代码?_Php_Templates_Layout_External - Fatal编程技术网

Php 如何配置页面以搜索外部布局代码?

Php 如何配置页面以搜索外部布局代码?,php,templates,layout,external,Php,Templates,Layout,External,我正在寻找让我的页面从外部模板页面搜索页面布局的方法。请参见下面的示例 <head> <title></title> </head> <body> <search for header, css, layout, etc from external page> Page contents <search for footer> </body> 页面内容 使用PHP或HTML有什么方法

我正在寻找让我的页面从外部模板页面搜索页面布局的方法。请参见下面的示例

<head>
<title></title>
</head>
<body>


<search for header, css, layout, etc from external page>

Page contents

<search for footer>


</body>

页面内容
使用PHP或HTML有什么方法可以做到这一点吗?我希望能够编辑所有页面的布局,而不必逐页进行编辑。我欢迎任何其他方法来达到同样的效果,只要它能在所有浏览器上运行


多谢各位

这正是PHP的用武之地。PHP脚本可以使用
include
语句包含另一个脚本的内容

因此,应用程序中的每个页面都可以有一个关联的PHP脚本来生成内容,并包括页脚布局的
footer.PHP
。这样,当您更改
footer.php
时,所有使用它的页面将自动获得更改

纯HTML无法做到这一点,尽管可以使用一些javascript和Ajax。我将设置两个基本示例


最简单的方法是,拥有多个由主文件调用的布局文件:

header.php:

<div id="header">
    Menu can go here.
    <?php echo 'I make all my files .php, so they can use PHP functions if needed.'; ?>
</div>

菜单可以在这里。
footer.php

<div id="footer">
    <a href="#">Footer Link</a>
</div>

index.php

<html>
    <head></head>
    <body>
        <?php include('/path/to/header.php'); ?>
        Specific index.php content here.
        <?php include('/path/to/footer.php'); ?>
    </body>
</html>
<?php include('/path/to/include.php'); ?>
<html>
    <head></head>
    <body>
        <?php echo makeHeader('Page Title'); ?>
        Specific index.php content here.
        <?php echo makeFooter(); ?>
    </body>
</html>

这里有具体的index.php内容。

另一个选项是使用一个PHP文件,其中包含函数中的所有不同布局元素。我之所以喜欢这个,是因为您可以包含一个文件,然后为不同的部分调用特定的函数。这也可以用来传递变量,比如页面的标题

layout.php

<?php
function makeHeader($title) {
    return 'My title is: '.$title;
}

function makeFooter() {
    $html = '
        <div id="footer">
            <a href="#">Footer Link</a>
        </div>
    ';
    return $html;
}
?>

index.php

<html>
    <head></head>
    <body>
        <?php include('/path/to/header.php'); ?>
        Specific index.php content here.
        <?php include('/path/to/footer.php'); ?>
    </body>
</html>
<?php include('/path/to/include.php'); ?>
<html>
    <head></head>
    <body>
        <?php echo makeHeader('Page Title'); ?>
        Specific index.php content here.
        <?php echo makeFooter(); ?>
    </body>
</html>

这里有具体的index.php内容。

只需确保使用相对路径(
nohttp://www.
)包含文件时。这将允许变量和函数平稳地传递。最简单的方法是使用PHP变量
$\u SERVER['DOCUMENT\u ROOT']
,因此,如果您有一个文件,您可以将其包含在
include($\u SERVER['DOCUMENT\u ROOT'.]./includes/layout.PHP')
中,无论您包含的文件位于何处