如何使用PHP包括在网站的所有页面中插入HTML存根 [英] How to use PHP include to insert an HTML stub across all pages in a website

查看:111
本文介绍了如何使用PHP包括在网站的所有页面中插入HTML存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的网站。它将有近20页。它有3级层次结构。

I am developing a simple website. It will have close to 20 pages. It has a 3 level hierarchy.

Home
MenuStub
   Category1
      Page1
      Page2
   Category2
      Page1
      Page2
   ....
      ....
      ....

主导航将有4-5个项目,代表每个'类别'
这对所有页面都是不变的。我甚至不打算以
突出显示当前类别或任何内容。

The main navigation will have 4 - 5 items representing each 'category' This will be constant for all the pages. I am not even planning to highlight the current category or anything.

之前我决定将菜单HTML stub单独放在一个单独的文件中
并使用PHP include将其包含在所有页面中。

Previously I decided to put the menu HTML stub alone in a separate file and use PHP include to include it in all pages.

但是,相对路径可能令人沮丧。假设菜单stub
文件位于根目录。

But, relative paths might be frustrating. Assume that the menu stub file is located at the root directory.

因此,在根级页面中,php include会读起来像

So, in the root-level pages, the php include will read like

include "menustub.html";

在二级页面中,应该说

include "../menustub.html";

在第三级页面中,应该说

and in third level pages, it should say

include "../../menustub.html";

首先,这是在一个页面中包含单个文件
的最佳方式网站?

First, is this the best way to include a single file across all pages in a website?

其次,如果网站变大,并且添加了更多级别
,维护这将是一个问题。如果我突然
决定将一整套页面向上或向下移动一个(或几个)
,我应该手动去改变每个文件中的相对路径

Second, if the website grows big, and many more levels are added, maintaining this will be a problem. If I suddenly decide to move an entire set of pages one (or several) levels up or down, I should manually go and change the relative paths in each file.

我在这里遗漏了什么吗?是否有通用的方法将
指向特定文件,每个页面都能理解,无论
位于何处?

Am I missing something here? Is there a universal way to point to a particular file, that every page will understand, regardless of where it is located?

什么是最好的方法是在没有这些维护噩梦的情况下将存根包含在所有页面
中?

What is the best way to have a stub and include it in all the pages without having these maintenance nightmares?

推荐答案

它使索引文件执行所有包含操作会更加动态,并使用GET参数告诉它要包含哪个页面。

It would be much more dynamic to have the index file do all the including, and use GET parameters to tell it which page is to be include.

例如,考虑一个目录结构如下:

For example, consider a directory structure like this:

/
  index.php
  /ui
    header.html
    menu.html
    footer.html
    Cat1/
      Page1.html
      Page2.html
    Cat2/
      Page3.html
      Page4.html

如果你总是调用索引文件,包括类别和页面的名称你想看,如: index.php?category = Cat1& page = Page1 ,你可以这样做:

If you were to always call the index file, including the name of the category and page you wanted to see, like: index.php?category=Cat1&page=Page1, you could do something like this:

<?php
// Include a header and a menu, defined in their own HTML files.
include('ui/header.html');
include('ui/menu.html');

// Set up a list of valid categories and their sub pages.
$pages = array(
    'Cat1' => array(
        'Page1',
        'Page2'
    ),
    'Cat2' => array(
        'Page3',
        'Page4'
    )
);

// Find the category and page to use.
if(isset($_GET['category'], $pages[$_GET['category']])) {
    $category = $_GET['category'];
} else {
    $category = 'Cat1';
}
if(isset($_GET['page'], $pages[$category][$_GET['page']])) {
    $page = $_GET['page'];
} else {
    $page = 'Page1';
}

// Include the selected content page.
include("ui/{$category}/{$page}.html");

// Include a footer
include('ui/footer.html');
?>

通过这种方式,您可以根据需要扩展内容,而无需重复包含每一个新文件。

This way you can expand the content as far and deep as you want without having to repeat your includes in every single new file.

这篇关于如何使用PHP包括在网站的所有页面中插入HTML存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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