cherry-project/templater

Cherry-project 模板引擎

v1.1.0 2019-05-11 18:55 UTC

This package is auto-updated.

Last update: 2024-09-12 06:46:45 UTC


README

Cherry-project 模板包装器

GitHub license

GitHub release

Packagist Version

包括

通过composer安装 composer require cherry-project/templater

在主文件中包含Autoloader (例如:index.php)

require_once __DIR__ . '/vendor/autoload.php';

Templater 类

导入类

use Cherry\Templating\Templater;

创建类新对象

$templateEngine = new Templater(PATH_TO_TEMPLATES);

其中 PATH_TO_TEMPLATES 是模板文件夹的路径。例如:__DIR__ . '/../examples/templates'

在您的模板文件夹中创建新的模板(例如:index.templater.php),其中包含简单的HTML标记和PHP

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello, World!</title>
</head>
<body>
    <h1>Hello, {{ name }}!</h1>
</body>
</html>

然后您可以使用两个参数调用 $templateEngine 对象的 render 方法

  • 渲染模板的名称
  • 模板的参数(PHP变量)

参数是简单的PHP数组

$args = [
    'name' => 'Name 1',
    'surname' => 'Surname 1'
];

我们的 index.templater.php 模板只包含一个PHP变量 $name,因此我们必须在 render 方法中传递它

$response = $templateEngine->render('index.templater.php', [
    'name' => 'Temuri'
]);

之后,$response 变量将包含响应对象,我们可以打印它

echo $response;

render 方法的第一个参数中,我们可以放置模板的完整文件名(例如:index.templater.php)或仅模板名称(不带文件扩展名的名称,例如:index

2019 © Cherry-project