zareismail/compilex

一个 Laravel Nova 工具。

v1.0.0 2023-05-29 19:46 UTC

This package is auto-updated.

Last update: 2024-08-29 22:17:29 UTC


README

显示数据

指令

自定义

简介

欢迎使用 Compilex 文档——一个强大的 PHP 视图编译包,旨在简化动态模板渲染过程。使用 Compilex,PHP 开发者可以轻松编译模板、替换变量和处理逻辑语句,所有操作均在字符串上下文中完成。本包旨在提供灵活高效的内容生成解决方案,让开发者能够专注于打造卓越的用户体验。

在本文档中,您将找到全面的信息、示例和指南,以帮助您理解并利用 Compilex 的功能。无论您是视图编译的新手还是希望提升现有工作流程,本文档都将作为宝贵资源,帮助您了解功能并发挥 Compilex 的全部潜力。

让我们开始吧,一起踏上简化 PHP 视图编译的旅程!

入门

您可以通过运行以下命令通过 Composer 安装 compilex

composer require zareismail/compilex

安装后,您可以使用编译器如下

$compiler = new \Zareismail\Compilex\Compilex();

$result = $compiler->compile('complex string containing patterns', [/* Your variables */]);

显示变量

您可以通过在变量周围包裹花括号来显示传递给您的 Compilex 模式的数据。例如,您可以这样显示 name 变量的内容

Hello, {{ name }}.

例如

echo $compiler->compile('Hello, {{ name }}.', ['name' => 'COMPILEX']);
// Output: Hello, COMPILEX.

多个变量

您也可以使用 or 运算符显示多个变量的有效值

Hello, {{ name or firstname or lastname }}.

例如

echo $compiler->compile('Hello, {{ name or firstname or lastname }}.', ['firstname' => 'COMPILEX']);
// Output: Hello, COMPILEX.

默认值

如果变量缺失,您可以使用 引号字符串 显示默认值

Hello, {{ name or '--' }}.

例如

echo $compiler->compile('Hello, {{ name or "--" }}.');
// Output: Hello, --.

指令的基本知识

默认情况下,Compilex 支持基本指令。所有 Compilex 指令都有以下语法

{% directive statement %} expression {% enddirective %}

其中,directive 可以是默认指令或任何 自定义指令statement 应满足指令要求,而 expression 可以是任何可渲染的字符串。

条件语句

条件语句非常有用,可以根据条件渲染或隐藏表达式。默认情况下,我们有两个条件语句:ifunless。您可以使用 if 指令在条件为真时渲染括号内的表达式,并使用 unless 指令在条件为假时渲染括号内的表达式。

条件语句遵循以下结构

{% if leftOperand comparator rightOperand %} expression {% endif %}
{% unless leftOperand comparator rightOperand %} expression {% endunless %}

此外,条件语句支持以下比较运算符

  • 相等运算符:===eqequalis
  • 不等运算符:>gtgreater than
  • 部分相等运算符:>=gtegreater than or equal

您可以通过更改指令(if/unless)生成其他比较。以下是一些示例

echo $compiler->compile('{% if a > b %} a is greater than b {% endif %}', ['a' => 1, 'b' => 2]);
// Output: a is greater than b

echo $compiler->compile('{% unless b > a %} a is greater than b {% endunless %}', ['a' => 1, 'b' => 2]);
// Output: a is greater than b

echo $compiler->compile('{% if a == b %} a is equal to b {% endif %}', ['a' => 1, 'b' => 1]);
// Output: a is equal to b

echo $compiler->compile('{% unless a == b %} a is not equal to b {% endunless %}', ['a' => 1, 'b' => 2]);
// Output: a is not equal to b

正面语句

有时您可能只需要在变量具有有效值时渲染条件语句。对于这种情况,您可以更改条件语句的结构如下

// to render enclosed epression for valid conditions
{% if variableName %} expression {% endif %}
// to render enclosed epression for invalid conditions
{% unless variableName %} expression {% endunless %}

例如

echo $compiler->compile('{% if a %} a has a valid value {% endif %}', ['a' => true]);
// Output: a has a valid value

echo $compiler->compile('{% unless b %} a doesn't have a valid value {% endunless %}', ['a' => false]);
// Output: a doesn't have a valid value

循环

Compilex 还支持以下结构的 loop 语句

{% each valueName, indexName of/in variableName %} expression {% endeach %}

在循环结构中,valueNameindexName 分别持有迭代变量的 valueindex。您可以使用这些名称在循环表达式中访问循环项和索引。variableName 是持有循环数据的属性名称,而 ofin 是循环结构的静态关键字。您还可以省略传递 index 名称,在这种情况下,您可以使用 index 关键字来访问循环 index

以下是一些示例

echo $compiler->compile('{% each item, key of items %} index {{ key }} holds {{ item }}, {% endeach %}', ['items' => [1,2]]);
// Output: index 0 holds 1, index 1 holds 2,

echo $compiler->compile('{% each name in names %} The {{ index }} name is: \'{{ name }}\', {% endeach %}', ['names' => ['Jack', 'Joe']]);
// Output: The 0 name is: 'Jack', The 1 name is: 'Joe',

嵌套语句

Compilex 的一个极好特性是支持嵌套指令。这意味着您可以在其他语句中使用任何语句,甚至可以在语句中使用语句。以下是一些示例

echo $compiler->compile('{% each item, key of items %} {% if key == 0 %} {{ item }} {% endif %} {% endeach %}', ['items' => [1,2]]);
// Output: 1

echo $compiler->compile('{% each numbers of groupedNumbers %} {% each number of numbers %} {{ number }}, {% endeach %} {% endeach %}', ['groupedNumbers' => [[1,2], [3,4]]]);
// Output: 1, 2, 3, 4,

echo $compiler->compile('{% if a > b %} {% if c > d %} I'm here {% endif %} {% endif %}', ['a' => 2, 'b' => 1, 'c' => 3, 'd' => 2]);
// Output: I'm here

自定义指令

如果您需要额外的指令,您可以通过使用 extend 方法轻松地定义自定义指令。以下是一个示例

$compiler->extend('any', function ($operand, $expression, $attributes = []) {
    foreach ((array) explode(',', $operand) as $attribute) {
        if ($this->hasAttribute($attribute) && $this->getAttribute($attribute)) {
            return $expression;
        }
    }
    return null;
});

您可以使用自定义指令如下

echo $compiler->compile('{% any a,b %} my directive is working {% endany %}', ['a' => false, 'b' => true]);
// Output: my directive is working

就是这样!有了这些指令和示例,您应该能够利用 Compilex 的力量来编译您的 PHP 视图。

祝您编码愉快!