?
このドキュメントでは、 php中國語ネットマニュアル リリース
原生 PHP 模板就是指直接用 PHP 來寫模板,這是很自然的選擇,因為 PHP 本身其實是個模板語言。這代表你可以在其他的語言中結合 PHP 使用,比如 HTML 。這對 PHP 開發(fā)者相當有利,因為不需要額外學習新的語法,他們熟知可以使用的函數,并且使用的編輯器也已經內置了語法高亮和自動補全。此外,原生的 PHP 模板沒有了編譯階段,速度會更快。
現今的 PHP 框架都會使用一些模板系統(tǒng),這當中多數是使用原生的 PHP 語法。在框架之外,一些類庫比如 Plates 或 Aura.View,提供了現代化模板的常見功能,比如繼承、布局、擴展,讓原生的 PHP 模板更容易使用。
原生 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') ?>
原生 PHP 模板使用繼承的示例
使用 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>