简体   繁体   中英

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.

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 %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM