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..