miladrahimi / phptemplate
免费PHP模板引擎,用于整洁且强大的项目!
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2020-02-09 18:43:11 UTC
README
PHP模板引擎!
概述
现代应用程序架构遵循用户界面和逻辑分离。应用逻辑层为用户界面层提供显示信息。因此,用户界面(视图)将与逻辑层(控制器)分离。
模板引擎提供了一种结构化的解决方案来实现这种分离。让我们看看!
用户界面是包含静态(HTML)和动态部分的模板文件。动态部分是模板引擎放置信息的地方。控制器将信息和模板名称传递给模板引擎。模板引擎返回编译后的模板(最终HTML)。控制器将结果发送给用户。
PHPTemplate语法是HTML、XML和基于标签文档的最佳语法。它被巧妙地设计,以便在网页设计师设计时看起来很漂亮。
安装
使用Composer(推荐)
如果您不熟悉Composer,请阅读如何在PHP项目中使用Composer文章。
在项目根目录中运行以下命令
composer require miladrahimi/phptemplate
手动
只要您的自动加载器遵循PSR-0或PSR-4标准,您就可以使用自己的自动加载器。只需将src
目录内容放入您的vendor目录中。
入门
以下示例展示了如何渲染profile.html
模板文件。
一个简单的模板文件示例
use MiladRahimi\PHPTemplate\TemplateEngineFactory;
$te = TemplateEngineFactory::create();
$te->setBaseDirectory("../Views");
$data = array(
"name" => "Bon",
"surname" => "Jovi"
);
echo $te->render("profile.html", $data);
以及profile.html
模板文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User Profile</title>
</head>
<body>
<h1>Profile</h1>
<p>Welcome {name} {surname}!</p>
</body>
</html>
短语
短语是简单的变量,将由模板引擎根据给定数据填充。如上例所示,{name}
和{surname}
是短语。
短语将通过PHP原生的htmlspecialchars()
函数进行HTML安全处理。如果您需要插入原始数据,如HTML片段,请在{
字符后放置!
。请参见以下示例
<div> {!content} </div>
灵活的模板
短语用于在视图(模板)中显示数据。但有时在显示之前必须控制或操纵数据。某些数据不总是显示,例如,当用户已经认证时显示“登录”按钮。某些数据必须重复显示多次,例如博客主页中的帖子。PHPTemplate提供布尔和数组标签来实现此类情况。
布尔标签
布尔标签用于在给定数据中,当标签名称作为元素为真时显示内容。
让我们扩展上述示例
use MiladRahimi\PHPTemplate\TemplateEngineFactory;
$te = TemplateEngineFactory::create();
$te->setBaseDirectory("../Views");
$data = array(
"name" => "Bon",
"surname" => "Jovi",
"is-admin" => true
);
echo $te->render("profile.html", $data);
以及profile.html
文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User Profile</title>
</head>
<body>
<h1>Profile</h1>
<p>Welcome {name} {surname}!</p>
<is-admin>You are admin!</is-admin>
</body>
</html>
在网页设计层面,布尔标签将在浏览器中显示。
顺序数组
PHPTemplate的HTML-like语法使使用数组变得非常简单。数组可以通过标签实现,就像布尔值一样。标签内容将重复与数组长度相同。就像PHP中的foreach
循环一样,您需要定义一个用于在主体中使用的当前数组元素的名称。在PHPTemplate中,您可以通过HTML-like的value
属性定义此名称。
use MiladRahimi\PHPTemplate\TemplateEngineFactory;
$te = TemplateEngineFactory::create();
$te->setBaseDirectory(__DIR__);
$data = array(
"name" => "David",
"surname" => "Gilmour",
"genres" => array("Progressive Rock", "Art Rock", "Blues Rock")
);
echo $te->render("singer.html", $data);
以下是singer.html
模板文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Favorite Singer</title>
</head>
<body>
<h1>{name} {surname}</h1>
<h2>Genres</h2>
<genres value="item">
{item} <br>
</genres>
</body>
</html>
genres
标签内容将重复与genres
数组长度相同。- 您可以通过在
value
属性中设置名称来访问当前元素,这里使用item
。 - 在网页设计层面,数组主体将只显示一次。
关联数组
关联数组可以通过其配对的键/值对而不是数组容器传递给模板引擎。当然,PHPTemplate仍然很好地支持它。
关联数组是一系列键/值元素。就像foreach()
循环一样,您需要定义两个名称,一个用于当前键,一个用于当前值。
在下面的示例中,我已定义album
作为当前键的名称,以及year
作为当前值的名称。
use MiladRahimi\PHPTemplate\TemplateEngineFactory;
$te = TemplateEngineFactory::create();
$te->setBaseDirectory(__DIR__);
$data = array(
"name" => "David",
"surname" => "Gilmour",
"genres" => array("Progressive Rock", "Art Rock", "Blues Rock"),
"albums" => array("On an Island" => 2006, "Rattle That Lock" => 2015)
);
echo $te->render("singer.html", $data);
以下是singer.html
模板文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Favorite Singer</title>
</head>
<body>
<h1>{name} {surname}</h1>
<h2>Genres</h2>
<genres value="item">
{item} <br>
</genres>
<h2>Albums</h2>
<albums key="album" value="year">
{album} : {year} <br>
</albums>
</body>
</html>
记录
记录是您需要一次性访问其所有元素的关联数组。记录概念在表格设计中非常有用。PHPTemplate使您像享用甜点一样轻松访问记录!
请参阅示例和包含一些记录的singles
数组
use MiladRahimi\PHPTemplate\TemplateEngineFactory;
$te = TemplateEngineFactory::create();
$te->setBaseDirectory(__DIR__);
$data = array(
"name" => "David",
"surname" => "Gilmour",
"genres" => array("Progressive Rock", "Art Rock", "Blues Rock"),
"albums" => array("On an Island" => 2006, "Rattle That Lock" => 2015),
"singles" => array(
array("track" => "There's No Way Out of Here", "album" => "David Gilmour"),
array("track" => "Blue Light", "album" => "About Face"),
array("track" => "Love on the Air", "album" => "About Face")
)
);
echo $te->render("singer.html", $data);
以下是singer.html
模板文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Favorite Singer</title>
</head>
<body>
<h1>{name} {surname}</h1>
<h2>Genres</h2>
<genres value="item">
{item} <br>
</genres>
<h2>Albums</h2>
<albums key="album" value="year">
{album} : {year} <br>
</albums>
<h2>Singles</h2>
<singles value="single">
<single>
{track} of the album {album} <br>
</single>
</singles>
</body>
</html>
- 不需要为记录定义键和值。
- 可以通过短语访问所有记录数组键。
- 这对于表格设计是一个很好的特性。
数据类型
PHPTemplate将所有数据转换为字符串或数组。
所有标量数据,如字符串、整数、浮点数等,都将转换为字符串。
所有具有__toString()
方法的对象都将转换为字符串。
所有数组和可遍历对象都将转换为数组。
所有闭包都将被调用,并且返回值将递归检查。
如果数据类型无法转换为字符串或数组,将抛出BadDataException
异常。
函数
避免在视图层使用逻辑指令是一种良好的做法。然而,您可能仍然需要在视图层访问您的应用程序API。您可以将闭包(如其他有效数据类型)传递给模板引擎。传递的闭包必须返回一个闭包或有效数据类型值,以便在短语中进行处理和替换。
请参阅以下示例中的date
元素
use MiladRahimi\PHPTemplate\TemplateEngineFactory;
$te = TemplateEngineFactory::create();
$te->setBaseDirectory(__DIR__);
$data = array(
"name" => "David",
"surname" => "Gilmour",
"genres" => array("Progressive Rock", "Art Rock", "Blues Rock"),
"albums" => array("On an Island" => 2006, "Rattle That Lock" => 2015),
"singles" => array(
array("track" => "There's No Way Out of Here", "album" => "David Gilmour"),
array("track" => "Blue Light", "album" => "About Face"),
array("track" => "Love on the Air", "album" => "About Face")
),
"date" => function () {
return date("Y/m/d");
},
);
echo $te->render("singer.html", $data);
以下是singer.html
模板文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Favorite Singer</title>
</head>
<body>
<h1>{name} {surname}</h1>
<h2>Genres</h2>
<genres value="item">
{item} <br>
</genres>
<h2>Albums</h2>
<albums key="album" value="year">
{album} : {year} <br>
</albums>
<h2>Singles</h2>
<singles value="single">
<single>
{track} of the album {album} <br>
</single>
</singles>
<p>Today: {date}</p>
</body>
</html>
作用域
作用域会覆盖高级作用域中具有相同名称的数据。
例如,以下示例中的singer
记录将覆盖name
和surname
。
use MiladRahimi\PHPTemplate\TemplateEngineFactory;
$te = TemplateEngineFactory::create();
$te->setBaseDirectory(__DIR__);
$data = array(
"name" => "Selena",
"surname" => "Gomez",
"others" => array(
array("name" => "Taylor", "surname" => "Swift"),
array("name" => "Demi", "surname" => "Lovato")
)
);
echo $te->render("singer.html", $data);
以下是singer.html
模板文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Young Singers</title>
</head>
<body>
<h1>Young Singers</h1>
Best Singer: {name} {surname}. <br>
Others: <br>
<others value="singer">
<singer>{name} {surname}</singer><br>
</others>
</body>
</html>
导入外部模板文件
您可能需要导入外部文件。
请参阅以下示例,其中page.html
导入head.html
文件。
use MiladRahimi\PHPTemplate\TemplateEngineFactory;
$te = TemplateEngineFactory::create();
$te->setBaseDirectory(__DIR__);
$data = array(
"page_title" => "Roger Waters Biography",
"name" => "Roger",
"surname" => "Waters",
);
echo $te->render("page.html", $data);
以下是page.html
模板文件
<!DOCTYPE html>
<html lang="en">
<import file="head.html">
<body>
<p>Welcome {name} {surname}!</p>
</body>
</html>
以下是head.html
模板文件
<head>
<meta charset="UTF-8">
<title>{page_title}</title>
</head>
框架集成
您可以使用Composer安装PHPTemplate。虽然所有现代PHP框架都支持Composer包,但您可以在大多数流行的框架中轻松使用PHPTemplate。
许可
PHPTemplate 由 Milad Rahimi 创建,并在 MIT 许可证 下发布。