Reputation: 6319
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
Reputation: 1482
You could simply do a
{$error|default:''}
You don't need an {if}{/if}
for that :)
Upvotes: 2
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
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