user1564141
user1564141

Reputation: 6319

Exception and smarty

I have block try - catch and i want assign error to the template to $error. I tried:

catch (Exception $e) {
   $smarty->assign("error", 'Error! Details: '.$e->getMessage());
}

Also tried:

catch (Exception $e) {
   $error = $e->getMessage());
}
$smarty->assign("error", $error);

But when there is no error, smarty requires this variable and all crashes. Is there any way to deal with it without using if? Or may be i am doing wrong from the begining?

Upvotes: 1

Views: 2896

Answers (3)

Emmanuel Okeke
Emmanuel Okeke

Reputation: 1482

You could simply do a

{$error|default:''}

You don't need an {if}{/if} for that :)

Upvotes: 2

Julien
Julien

Reputation: 1980

Try this
Php:

catch (Exception $e) {
   $smarty->assign("error", true);
   $smarty->assign("error_message", $e->getMessage());
}

Smarty:

{if $error}
       {$error_message}
{/if}

Upvotes: 0

rodneyrehm
rodneyrehm

Reputation: 13557

How about having the template check if the $error variable is defined?

{if !empty($error)}
  Oops: {$error}
{/if}

otherwise you can re-assign variables:

$smarty->assign("error", null);
try {
  // …
} catch (Exception $e) {
  $smarty->assign("error", $e->getMessage());
}

Upvotes: 0

Related Questions