Dail
Dail

Reputation: 4608

How can I check if a variable exists in Smarty?

I'm using Smarty template engine.

I'm doing a simple login page. I set a variabile named error with a message if there are some problems, but IF NOT I get:

Notice: Undefined index: error

How could I check if this variable exists?

I only do:

{if $error}<h1>{$error}</h1>{/if}

thanks

Upvotes: 31

Views: 49154

Answers (4)

r_a_f
r_a_f

Reputation: 649

You can also use:

{if $error|default}<h1>{$error}</h1>{/if}

"|default" modifier check if variable exist and accept one param (default: empty string)

Upvotes: 3

Wesley van Opdorp
Wesley van Opdorp

Reputation: 14941

There you go!

{if isset($error)}
    {* TODO something *}
{/if}

Upvotes: 67

Dumindu Perera
Dumindu Perera

Reputation: 1649

This is short :) No warnings or errors.

{if $error}

Upvotes: -3

Wasim Karani
Wasim Karani

Reputation: 8886

isset() - smarty - php

isset($error)

Upvotes: 57

Related Questions