?
This document uses PHP Chinese website manual Release
原生 PHP 模板就是指直接用 PHP 來寫模板,這是很自然的選擇,因?yàn)?PHP 本身其實(shí)是個(gè)模板語言。這代表你可以在其他的語言中結(jié)合 PHP 使用,比如 HTML 。這對(duì) PHP 開發(fā)者相當(dāng)有利,因?yàn)椴恍枰~外學(xué)習(xí)新的語法,他們熟知可以使用的函數(shù),并且使用的編輯器也已經(jīng)內(nèi)置了語法高亮和自動(dòng)補(bǔ)全。此外,原生的 PHP 模板沒有了編譯階段,速度會(huì)更快。
現(xiàn)今的 PHP 框架都會(huì)使用一些模板系統(tǒng),這當(dāng)中多數(shù)是使用原生的 PHP 語法。在框架之外,一些類庫(kù)比如 Plates 或 Aura.View,提供了現(xiàn)代化模板的常見功能,比如繼承、布局、擴(kuò)展,讓原生的 PHP 模板更容易使用。
原生 PHP 模板的簡(jiǎn)單示例
使用 Plates 類庫(kù)。
<?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') ?>
原生 PHP 模板使用繼承的示例
使用 Plates 類庫(kù)。
<?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>