Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use if not statement with twig

Tags:

twig

I have been trying to figure out how to use the if not clause with Twig template engine.

According to the documents:

You can also use not to check for values that evaluate to false:

So I'm trying to use it like this:

{% if not signed_in %}
 You are not signed in.
{% endif %}

And signed_in comes from a middleware. Looking like this:

    if (!$this->container->auth->check()) {
        /* Render a Sign-up */
        return $this->container->view->render($response, 'subscriber/index.twig', ['signed_in' => false]);
    }

So if I {{ dump(signed_in) }} when I'm not signed in it returns false. But when I'm signed in, singed_in returns null.

Not signed in left. Signed in on right

The current solution I'm going with is {% if signed_in is defined %}, but that is actually not what I'm looking for, or is it?

like image 327
Adam Avatar asked Mar 11 '23 20:03

Adam


1 Answers

Twig != is a comparison operator.

The following comparison operators are supported in any expression: ==, !=, <, >, >=, and <=

You can use != to compare any two things that php lets you compare, and what you get back is a boolean.

Twig not is a logic operator. (so are and and or)

not: Negates a statement.
like image 70
Prasant Kumar Avatar answered Apr 26 '23 10:04

Prasant Kumar