现今的 PHP 框架都会使用一些模板系统,这当中多数是使用原生的 PHP 语法。在框架之外,一些类库比如 Plates 或 Aura.View,提供了现代化模板的常见功能,比如继承、布局、扩展,让原生的 PHP 模板更容易使用。
使用Plates 类库。
<?php // user_profile.php ?>
<?php $this->insert('header', ['title' => 'User Profile']) ?>
<h1>User Profile</h1>
<p>Hello, <?=$this->escape($name)?></p>
<?php $this->insert('footer'); ?>
使用Plates 类库。
<?php // template.php ?>
<html>
<head>
<title><?=$title?></title>
</head>
<body>
<main>
<?=$this->section('content')?>
</main>
</body>
</html>
<?php // user_profile.php ?>
<?php $this->layout('template', ['title' => 'User Profile']) ?>
<h1<User Profile</h1>
<p>Hello, <?=$this->escape($name)?></p>