Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phalcon volt bitwise operatios?

I need to run the following logic in the volt templates however it seems to be it doesnt support it. ANy ideas on workarounds ?

{% for index, p_key in partner_var %}
 <input id="{{ key }}[]" name="{{ key }}[]" value="{{ p_key.id }}" type="checkbox"  
{% if user.p_body  & (1 << (p_key.id - 1)) %}
   checked
{% endif %}>
{{ p_key.title }} 

fails with the Error Scanning error before ' (1 << (p_key.id.

like image 972
mahen3d Avatar asked Sep 27 '22 03:09

mahen3d


1 Answers

You are correct, Volt does not support bitwise operators. One of workarounds is to create your functions when declaring voltService:

$di->setShared('view', function() {

    $view = new \Phalcon\Mvc\View();

    $view->registerEngines(array(
        '.volt' => 'voltService'
    ));

    return $view;
});

$di->set('voltService', function ($view, $di) {
    // ...

    $volt = new Phalcon\Mvc\View\Engine\Volt($view, $di);
    // ...

    $compiler = $volt->getCompiler();

    $compiler->addFunction('bit_and', function($resolvedArgs, $exprArgs) use ($compiler) {

        return sprintf(
            '(%s & %s)',
            $compiler->expression($exprArgs[0]['expr']),
            $compiler->expression($exprArgs[1]['expr'])
        );

    });

    return $volt;
});

to use as function in Volt template

{% if bit_and(2, keyword.getFlags()) %}
    checked="checked"
{% endif %}
like image 93
yergo Avatar answered Oct 18 '22 12:10

yergo