1


I have an bunch of HTML files that function as templates for my webapp. Upon loading of the page the files are cached/loaded in the browser and rendered in the DOM on demand according to the app router. Some of these template files should however include content variable to database entries. I have therefore denoted the location of this data in the templates using a specific pattern (handlebars) and I run the following PHP code (inside a class) to replace the locations with data from the database:

$template = '<div>{{user_name}}</div>'; //template loaded with fopen 
$this -> data['user_name'] = 'John Smith'; //data from server
$template = preg_replace_callback(
    '/{{([a-zA-Z0-9_]+)}}/',
    function($matches){
        $data = $this -> data;
        return (isset($data[$matches[1]])?$data[$matches[1]]:"");
    },
    $template);

I was wondering if it is possible to have a central function in PHP that runs through all the HTML files and 'compiles' (using the above) before serving them to the front-end? I realize that you could simply make each .html template into a .php file and preform the operations within and echo the result, but I feel that there must be a more refined/scalable solution..

Bjoern
  • 15,934
  • 4
  • 43
  • 48
Andreas Jarbol
  • 745
  • 2
  • 11
  • 27
  • If you have just static html files lying around, you can [parse HTML files as PHP](http://stackoverflow.com/questions/7181853/parse-html-as-php), and you can also [run a PHP function after every request](http://stackoverflow.com/questions/1938183/php-function-or-file-to-run-before-and-after-every-request). – Denis Oct 04 '13 at 12:09
  • Ok, however I would need to add PHP script to the template html files, and I would like to keep these pure html hence the handlebars in the first place.. – Andreas Jarbol Oct 04 '13 at 12:27
  • I suggested to [run a function after every request](http://stackoverflow.com/questions/1938183/php-function-or-file-to-run-before-and-after-every-request), regardless of file contents. Please take a look at my link. – Denis Oct 04 '13 at 12:28
  • At what point did you want to compile these files? On demand; JIT as it were? Then there wouldn't be much difference in efficiency with using an ordinary PHP file. Or did you want to compile them beforehand and keep them around as static HTML documents until they are requested? – Mr Lister Oct 04 '13 at 12:49
  • `did you want to compile them beforehand and keep them around as static HTML documents until they are requested` yes that's what I'm trying to achieve.. – Andreas Jarbol Oct 04 '13 at 14:06
  • I could probably do what you are saying @Denis, but how do I get all the `HTML` into a `PHP` string and processed, with the script included before or after the request? Could you do something like file_get_html('this'), and in that case, what would 'this be'? – Andreas Jarbol Oct 04 '13 at 14:39
  • You use ob_start with a callback at the beginning. – Denis Oct 04 '13 at 14:41
  • Ohh sneaky, but the HTML already included in the file would need to be overwritten, or at least deleted and the ob cache echo'ed, how should I approach that? Btw. thanks for the great comments - i understand now :) – Andreas Jarbol Oct 04 '13 at 14:51

0 Answers0