Include html while keeping paths relative to file being included
Have several html snippets that I want included in a 'parent' file.
The parent file contains relative paths. These snippets also contain relative paths, relative to their location - not relative to the file they will be included into. For example, one snippet is the header, common to all pages on the site.<div style='background:url(img/bg.jpg)'>
<img src='img/logo.png'>
</div>
This will be inserted into the existing page as follows:
<div style='background:url(img/gradient.jpg)'>
<?php include '../includes/header.txt'; ?>
</div>
Example is very greatly simplified, the actual files have many paths.
I am NOT in control of the files that will be included. Which means I cannot add PHP variables to the urls.I also cannot make the paths absolute in any way.
For the included files - because I have no control over the html. For all files - because the final pages are dev'd on several machines, and must work whether at http://www.mysite/myfile, http://localhost/mysite/myfile, or even http://127.0.0.1/~/anyuser/...Some ideas I've had:
- php include:
include '../includes/header.txt';
- Paths come out relative to the file it has been included into.
- Including each snippet as an iFrame:
- Page layout isn't very iFrame friendly. Besides SEO & page load issues.
- Parsing the page and replacing all paths using a parser.
- The pages are not 100% valid HTML (no control over them) and the parser coughs and rolls over dead. Besides the ridiculous server load.
- Using base tags
- Works beautifully in every real browser. But Internet Explorer 7, 8, and 9 just ignore the base tag (outside the head). One base tag could be declared in the head, but then all the other paths on the page (and probably in the CSS files) will be all wrong.
BaseTag Usage:
<?php
echo "<base href='$path_to_includes_folder' />";
include '../includes/header.txt';
echo "<base href='$path_based_on___file__' />";
?>
As I understand, this exactly why the base tag was created, and is supported in every decent browser - so that we can, in the middle of one HTML file, tell the browser that I am about to include a second HTML file, 开发者_如何学JAVAand all paths should be relative to the new location. This worked beautifully in IE6, and I assume there must have been some logic in dropping support for it. Some logic which escaped Opera, Webkit, and Mozilla. The only posts I can find on the subject laud Microsoft for dropping support, without giving even a hint of a reason why dropping support for something which is an accepted standard (in use in all browsers of the time) and useful is a good thing.
Now, I hate developing for IE with a passion, but cannot ignore it. They still have almost 1/3 of the users!
So, how to include php snippets with relative paths, and keep them relative to the file being included?
And can anyone please tell me if there is any reason at all MS dropped base support, or what can be done to help get it back?
For these situations I use the following,
$PROTOCOL = (!empty($_SERVER['HTTPS'])) ? 'https' : 'http';
$DOC_ROOT = $PROTOCOL.'://'.$_SERVER['SERVER_NAME'];
//The project path points to the root file (index.php, or whatever your index file is).
$projectRoot = dirname($DOC_ROOT.$_SERVER['SCRIPT_NAME']).'/';
Then you could do (using your example)
<div style='background:url(<?php echo $projectRoot; ?>img/bg.jpg)'>
<img src='img/logo.png'>
</div>
This will give you a dynamic absolute path.
精彩评论