Christian
Christian

Reputation: 321

Volt template check type of object

VOLT template gives the possibility, to check the type of an object:

{% set external = false %}
{% if external is type('boolean') %}
    {{ "external is false or true" }}
{% endif %}

Is there a possibility, to check if the object is type of a model like this:

{% if user is type('user') %}
    {{ "user is type of user" }}
{% endif %}

Thanks for your help

Upvotes: 1

Views: 617

Answers (2)

rabudde
rabudde

Reputation: 7722

As of today (2021), there still seems to be no solution in Phalcon. To solve this for me, I've added a custom Volt function:

$compiler = $this->volt->getCompiler();
$compiler->addFunction('instanceof', function ($resolvedArgs, $exprArgs) {
    $object = $instance = 'null';
    if (isset($exprArgs[0])) {
        $object = $exprArgs[0]['expr']['value'] ?? 'null';
    }
    if (isset($exprArgs[1])) {
        $instance = $exprArgs[1]['expr']['value'] ?? 'null';
    }
    return '$' . $object . ' instanceof ' . $instance;
});

In Volt, I'm able to check instance of object now with:

{% if instanceof(myobject, \DateTime) %}
{% endif %}

Upvotes: 0

Piotr
Piotr

Reputation: 29

You must create a functiton in services.php here

Upvotes: -1

Related Questions