uuf6429/expression-language-tplstring
最新稳定版本:2.0.0
Composer 安装命令:
composer require uuf6429/expression-language-tplstring
包简介
Template String support for Symfony Expression Language
README 文档
README
🪡 Template Strings
for Symfony Expression Language (4-6)
What looks like a dot, a cross and a wave, and does the same thing?
It's the concatenation operator, of course!
PHP uses a dot/period (
.), many languages including javascript use+, whereas Symfony Expression Language uses the tilde (~).
This library provides a translation layer on top of Expression Language that converts template strings in ES6 format* to valid expression. While an updated Expression Language subclass is provided for convenience, you don't have to use it, and you can use the provided trait instead.
* only ES6 string interpolation (with any expressions and nesting) is supported; f.e. tagged templates are not.
🔌 Installation
As always, the recommended and easiest way to install this library is through Composer:
composer require "uuf6429/expression-language-tplstring"
🚀 Usage
If you do not plan on extending Symfony Expression Language class, you can use the provided drop-in:
$el = new \uuf6429\ExpressionLanguage\ExpressionLanguageWithTplStr(); $el->evaluate('`hello ${name}!`', ['name'=>'mars']); // => hello mars!
Otherwise, you can subclass the desired Expression Language class and use the provided trait:
class MyEL extends \uuf6429\ExpressionLanguage\ExpressionLanguageWithArrowFunc { use \uuf6429\ExpressionLanguage\TemplateStringTranslatorTrait; public function compile($expression, array $names = []) { if (!$expression instanceof \Symfony\Component\ExpressionLanguage\ParsedExpression) { $expression = $this->translateTplToEl($expression); } return parent::compile($expression, $names); } public function evaluate($expression, array $values = []) { if (!$expression instanceof \Symfony\Component\ExpressionLanguage\ParsedExpression) { $expression = $this->translateTplToEl($expression); } return parent::evaluate($expression, $values); } } $el = new MyEL(); $el->evaluate( 'users.map((user) -> { `hello ${user.name}!` }).join(` `)', [ 'users' => new \Illuminate\Support\Collection([ (object)['name' => 'John', 'surname' => 'Doe'], (object)['name' => 'Jane', 'surname' => 'Roe'], ]) ] ); // => hello John! hello Jane!
统计信息
- 总下载量: 5.9k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-07-17