PHP code example of zareismail / compilex

1. Go to this page and download the library: Download zareismail/compilex 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/ */

    

zareismail / compilex example snippets


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

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

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

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

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

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

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',

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

$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