tpawl/lite

轻量级模板引擎

v1.5.0 2019-03-28 14:23 UTC

This package is auto-updated.

Last update: 2024-09-17 13:14:26 UTC


README

Lightweight Template Engine

Build Status Coverage Status

描述

LiTE 是一个专门适用于后端的临时模板引擎。它是本地的,因为它使用 PHP 作为其模板语言,无需编译模板。它支持模板变量和视图助手。

要求

LiTE 需要 PHP 7.3+

安装

通过 Composer

$ composer require tpawl/lite

使用

开发者版的 LiTE

基础知识

LiTE 的核心是一个名为 模板表达式 的对象(类 TPawl\LiTE\Expressions\TemplateExpression)。

创建模板表达式对象的示例

$settings = [
    $template, // a string holding the template
    ['the' => 'variables', 'go' => 'here'],
    '/path/to/view_helpers',
    'view_helpers\namespace',
];

$templateExpression = new TPawl\LiTE\Expressions\TemplateExpression($settings);

这将创建一个模板表达式,该表达式在 /path/to/view_helpers/ 文件夹中查找视图助手。

请注意,模板表达式的唯一参数是设置选项的数组。

  • 第一个选项是模板作为字符串。
  • 第二个选项是模板变量的关联数组,其中变量的名称是键,其值是。
  • 第三个选项是存储视图助手的文件夹路径。
  • 第四个选项是定义视图助手的命名空间,作为字符串。如果您不使用命名空间来存储视图助手,请在这里写入空字符串('')。

输出模板

$templateExpression->display();

定义视图助手

视图助手是一个实现了 TPawl\LiTE\ViewHelperInterface 接口的类,该接口具有静态的 execute 方法。该类的名称必须以 ViewHelper 结尾,并且第一个字母必须是大写。此类的代码必须保存为与相应类同名的文件,并以 .php 结尾。此文件必须存储在模板表达式的第三个配置选项指定的文件夹中。

示例

<?php
// HelloViewHelper.php

class HelloViewHelper implements TPawl\LiTE\ViewHelperInterface
{
    public static function execute(array $arguments): void
    {
        print 'Hello world.';
    }
}

$arguments 是可以传递给视图助手的参数的索引数组。在视图助手中的输出可以使用 echoprintprintf 等。

子模板表达式

TPawl\LiTE\Expressions\SubTemplateExpression 的对象旨在在视图助手内部使用。构造函数接受两个参数

  • 第一个参数是模板作为字符串。
  • 第二个参数是模板变量的关联数组,其中变量的名称是键,其值是。

示例

use TPawl\LiTE\Expressions\SubTemplateExpression;

class ExampleViewHelper implements TPawl\LiTE\ViewHelperInterface
{
    public static function execute(array $arguments): void
    {
        $condition = $arguments[0];

        if ($condition) {

            $subTemplateExpression = new SubTemplateExpression(
                $templateA, ['the' => 'variables', 'go' => 'here']);

            $subTemplateExpression->display();

        } else {

            $subTemplateExpression = new SubTemplateExpression(
                $templateB, ['the' => 'variables', 'go' => 'here']);

            $subTemplateExpression->display();
        }
    }
}

杂项

要确定您是否在视图助手内部或外部,可以使用静态方法 TPawl\LiTE\Context\Context::isEmpty()。如果在视图助手内部,则返回 false,否则返回 true

定义了类 TPawl\LiTE\Version,它包含 LiTE 的版本信息。它定义了以下常量

  • MAJOR 用于主版本号(主要发布)。
  • MINOR 用于次版本号(次要发布)。
  • REVISION 用于修订号(补丁级别)。

示例

use TPawl\LiTE\PackageInformations;

echo 'Powered by ', PackageInformations::PACKAGE_NAME , ' ', PackageInformations::makePackageVersionString();

echo 'Copyright &copy; ', PackageInformations::PACKAGE_COPYRIGHT['years'], ' by ', PackageInformations::makePackageCopyrightHoldersString();

为模板设计师的 LiTE

模板变量

<?php $this->foo; ?>

这定义了一个名为 foo 的模板变量。上面的完整表达式被替换为模板变量 foo 的值。

视图助手

<?php self::bar(); ?>

这调用没有参数的视图助手 BarViewHelper。如果您有参数,您必须将它们作为逗号分隔的列表写入在 bar 后面的括号中。例如:<?php self::bar('arg1', 'arg2', ...); ?>

上面的完整表达式被替换为视图助手 BarViewHelper 的输出。

预定义视图助手

这里有一个预定义的视图助手 _xmlViewHelper。它的调用方式如下

<?php self::_xml('version="1.0" encoding="UTF-8" standalone="yes"'); ?>

这将转换为 <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>。如果您想输出XML,这很有用。

示例

将以下视图助手保存到任何文件夹中的名为 MsgViewHelper.php 的文件中

<?php

class MsgViewHelper implements TPawl\LiTE\ViewHelperInterface
{
    public static function execute(array $arguments): void
    {
        if (version_compare(PHP_VERSION, '7.1.0') >= 0) {

            $msg = 'Everything is fine';

        } else {

            $msg = 'This is not good';
        }
        print $msg;
    }
}

假设您在上面的视图助手所在的同一个文件夹中有一个以下脚本

<?php

require_once '/path/to/vendor/autoload.php';

$template = <<<'HTML'
<!DOCTYPE html>
Hello <?php $this->name; ?>!<br>
You are running PHP <?php $this->ver; ?>: <?php self::msg(); ?>
HTML;

$variables = [
    'name' => 'Thomas',
    'ver' => PHP_VERSION,
];

$settings = [
    $template,
    $variables,
    '.',
    '',
];

$templateExpression = new TPawl\LiTE\Expressions\TemplateExpression($settings);

$templateExpression->display();

如果在浏览器中调用上述脚本,可能的输出之一可能是

Hello Thomas!
You are running PHP 7.1.10: Everything is fine