简体   繁体   中英

Simple HTML include system (Nested includes)

My html files are becoming very huge as I'm writing a big html app and I want to organised it. I have an array that will include all the includes that looks like this:

$html_includes = [
  'menu',
  'tools',
  'canvas',
  'sidebar' => [
    "resource",
    "layers"
  ],
  "footer"
];

What I'm trying to do is create an function to include all the files and if there's a folder (eg sidebar) include <folder>.html and create a callback to include the files withing the <folder>.html file. Here my attempt so far:

function include_html($includes,$path=""){
    foreach($includes as $key => $value){
        if(is_array($value)){
            function callback(){
                include_html($value,($path.$key."/"));
            }
        } 
        include($path.$key.".html");
    }
}

include_html($html_includes);

Folder structure

\canvas.html
\menu.html
\tools.html
\sidebar
\sidebar\layers.html
\sidebar\resources.html
\sidebar\sidebar.html

sidebar.html

<div id="sidebar">
    <div id="sortable">
        <?php callback(); ?>
    </div>
</div>

Scope is the problem as callback doest not have access to $value/$key . What I maybe need maybe is Anonymous functions but I'm not sure if that's correct. Any help is welcome.

Is this a good way to organised code?

Short Answer: No.

You're maintaining a folder structure with all of your includes, and then you're maintaining a second system of arrays to manage it all. If your site grows any larger or more complex it's quickly going to become unmaintainable.

At the very least you should change all of these files to .php or set up your server to run .html files through the PHP interpreter. Then when one file needs to include another you can simply <?php include('otherfile.php'); ?> <?php include('otherfile.php'); ?> and everything that that file needs will be included, and so on down the chain without having to keep track of the structure separately.

This could probably also be accomplished with Server Side Includes, but IMHO that's something best avoided.

That said, constructing your site like this is only marginally better than static HTML files. You should definitely look into using some sort of framework.

This was the figured out function that I needed, using http://www.php.net/manual/en/functions.anonymous.php

function include_html($includes, $path = "") {
  foreach($includes as $key = > $value) {
      if (is_array($value)) {
          $callback = function () use($value, $key, $path) {
              include_html($value, ($path.$key."/"));
          };
          include $path.$key."/".$key.".html";
      } else include $path.$value.".html";
  }
}

include_html($html_includes, $html_path);

In sidebar.html changed from callback() -> $callback()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM