prgfx/fusion-template-literals

提供类似javascript标签模板字面量的语法的一个融合领域特定语言(DSL)

0.3.0 2020-01-05 10:05 UTC

This package is auto-updated.

Last update: 2024-09-05 20:27:26 UTC


README

基于javascript中标签模板字面量的Neos Fusion DSL实现。

composer require prgfx/fusion-template-literals

用法

默认情况下,此包定义了DSL标识符 plain,但您可以为您的应用程序的更有意义的标签对其进行别名。

p = Some.Example:Prototype {
    value = plain`Some text with ${I18n.translate('interpolated')} values`
}

此包包含两个实现:一个数组渲染器(创建一个类似数组的融合对象(例如 Neos.Fusion:ArrayNeos.Fusion:Join,具体取决于配置))和一个eel表达式渲染器。
后者具有优势,因为您可以像看起来那样直观地引用变量,例如 ${this.variable}

trackingId = ${Configuration.setting(...)}
snippet = inline`(w=> { w.qa=w.qa||[];w.qa.push('create', '${this.trackingId}');})(window);`

然而 这与多行块不太兼容,因为eel表达式没有正确输出换行符。这对于像上面显示的带有压缩模式 compress 的块(见下文)是不错的。毕竟,这个包主要针对此类场景。

多行块

给定第一行的块模式修饰符(以及这一行中没有其他内容),多行块可能会被解释得不同。可以在 Prgfx.Fusion.TemplateLiterals.blockDelimiters 中配置块模式修饰符以符合您的喜好。有不同多行块模式:

默认

仅截断周围的空行,其余保持不变

value = plain`
    line 1
    line 2
`
// will not contain the first and last "empty" line

将删除所有缩进

value = plain`|
    line 1
        line 2
    line 3
`
// will return 'line 1\n    line 2\nline3'

单行

将删除所有缩进,并将换行符与单个空格连接。
双换行符将创建一个换行符。

value = plain`>
    line 1
    line 2

    line 3
`
// 'line 1 line 2\nline3'

压缩

与单行相比,将删除每行的所有周围空白。

value = plain`>>
if (foo) {
    console.log(foo);
}
`
// 'if (foo) { console.log(foo); }'

自定义实现

此包的实现方式允许您轻松扩展 PlainTemplateLiterals 实现,并覆盖 generateCode 方法。此方法接收 stringParts: string[]...expressions: string[],就像javascript等价物一样。