PHP在服务于前端之前编译HTML文件

时间:2013-10-04 12:05:36

标签: php html templates precompile


我有一堆HTML个文件,可以作为我的webapp的模板。加载页面后,文件将在浏览器中缓存/加载,并根据应用程序路由器按需DOM呈现。但是,其中一些模板文件应包含数据库条目的内容变量。因此,我使用特定模式(把手)在模板中表示了此数据的位置,并运行以下PHP代码(在类中)以使用数据库中的数据替换位置:

$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);

我想知道是否有可能在PHP中运行所有HTML文件和'编译'(使用上面的内容),然后再将它们提供给前端? 我意识到你可以简单地将每个.html模板制作成.php文件并在其中执行操作并回显结果,但我觉得必须有更精细/可扩展的解决方案..

0 个答案:

没有答案