PHP code example of uuf6429 / expression-language-tplstring

1. Go to this page and download the library: Download uuf6429/expression-language-tplstring library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

uuf6429 / expression-language-tplstring example snippets


$el = new \uuf6429\ExpressionLanguage\ExpressionLanguageWithTplStr();
$el->evaluate('`hello ${name}!`', ['name'=>'mars']); // => hello mars!

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!