I'm working on population static HTML
with database information in PHP
. The HTML
files have a pattern ({{$pattern}}) which when located in an HTML
file will be replaced with a matching PHP
variable.
An example HTML
file could be:
<div>{{$apples}}</div>
To populate this HTML
file on demand, I've created the following PHP
script (prepend.inc
):
function ob_callback($buffer){
global $data;
$data['apples'] = CONSTANT; //global definition defined elsewhere
$buffer = preg_replace_callback(
'/{{\$([a-zA-Z0-9_]+)}}/',
function($matches){
global $data;
return (isset($data[$matches[1]])?$data[$matches[1]]:"");
},
$buffer);
return $buffer;
}
ob_start("ob_callback");
This scrip is prepended to every HTML
file by adding the following to the .htaccess
in the HTML
directory:
AddType application/x-httpd-php .html
php_value auto_prepend_file "prepend.inc"
Because of the architecture of my application, I require an HTML
file echo'ed in PHP
.
This has however proven to be a bigger task than expected because any attempt I make at converting the file to a PHP
string and echo'ing results in the above ob_callback
not being executed... (Althrough I know prepend.inc
is run)
I have tried fread
, file_get_content
and even:
const CONSTANT = 'oranges';
ob_start();
require 'theHTMLfile.html';
$out = ob_get_contents();
ob_end_clean();
echo $out;
None of these however seem to have reached the ob_start callback in prepend.inc, and have had the pattern replaced...
One solution would be to use the full URL (require 'http://www.example.com/theHTMLfile.html';
), but I CANNOT use that as it would break the reference to global variables, that I require to fetch data for the HTML
etc..
Any suggestions would be much appreciated!