时间:2023-12-20 13:06:02 | 来源:网站运营
时间:2023-12-20 13:06:02 来源:网站运营
我想用PHP写个网站,我又不想用Smarty模板,我怎么把php代码和html代码分离呢?:自己不怕死的过来打脸了。说好的周一,结果是11月之后的周二。<h1><?=$title?></h1><ul> <?php foreach($list as $value): ?> <li><?=$value?></li> <?php endforeach; ?></ul>
这个是解析文件:<?php/** * 模板解析 */class View{ protected $path; protected $vars; public function __construct($path, $vars = []){ if (is_file($path)) { $this->path = $path; } $this->vars = $vars; } public function fetch(){ ob_start(); ob_implicit_flush(0); extract($this->vars, EXTR_OVERWRITE); require_once $this->path; return ob_get_clean(); }}$view = new View('./index.html', ['title' => 'test', 'list' => ['a', 'b', 'c']]);echo $view->fetch();?>
就是这样了。完毕。各位可以过来说我了。关键词:分离,模板