Reputation: 357
I've got a little web project where all pages have a common header file they import before the actual page content. The header file is something like the following:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript" src="js/some_script.js"></script>
<link rel="shortcut icon" href="images/web.ico" />
</head>
Then all the pages have a <?php include("header.php"); ?>
, so they load the <head>
tags at the begining.
I'm now coding a new page, that also includes the previous header.php
, but it requires a second javascript to load. I can load it without issues using the <script>
tag in the middle of the page, but I was wondering if its possible to append it directly to the <head>
tag, instead of resting in the middle of the html code.
The actual code looks like something like this:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript" src="js/some_script.js"></script>
<link rel="shortcut icon" href="images/web.ico" />
</head>
<body>
<p>Some text here</p>
<script type="text/javascript" src="js/another_script.js"></script>
<p>Some more text</p>
</body>
</html>
And I would like to know how to achieve the following:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript" src="js/some_script.js"></script>
<link rel="shortcut icon" href="images/web.ico" />
<script type="text/javascript" src="js/another_script.js"></script>
</head>
<body>
<p>Some text here</p>
<p>Some more text</p>
</body>
</html>
I know this is possible using for example jQuery or the Simple HTML DOM, but I was wondering if I can achieve this without using any other external source.
Upvotes: 2
Views: 12907
Reputation: 12306
You may include header.php like this:
<?php
ob_start();
include("header.php");
$contents = ob_get_contents();
ob_end_clean();
echo str_replace('</head>', '<script type="text/javascript" src="js/another_script.js"></script></head>', $contents)
?>
Code is simple. You get contents of header.php to buffer, than add before of </head>
new script include and print this new content to browser.
Upvotes: 6
Reputation: 31634
Something we did for this was to put our drawing into a class and then have that class do all the output. So you could write a class like this and call a method to add things to the headers. Then it would get output all at once. Here's some pseudo code to give you a push in that direction. This way, your includes can call the addHeader
function and add in any extra files you need before you move to output. In other words, this is essentially a controller for your view.
class Draw {
protected headers = array();
public function addHeader($header) {
$this->headers[] = $header;
}
public function drawPage($page) {
$headers = $this->headers;
include 'top.php';
include $page . '.php';
include 'bottom.php';
}
}
Upvotes: 0