ReynierPM ReynierPM - 1 year ago 132
Smarty Question

How to avoid FATAL error in Smarty templates while we check for objects

I am performing the following check in a Smarty template:

{{if $Pricing->getCommission()}}
do something

This is part of the class

class Pricing {
protected $commission;

public function getCommission() {
return $this->commission;

$Pricing = new Pricing();

Then I use the
PHP object in the Smarty template. If
lacks of
property, accessed through
public method this will turn into a FATAL and the application will thrown this to the view or in the best case display a blank page. I want to avoid that, how? I can't change how values are received meaning I can't get rid of the object on the template. Any advice?

Answer Source

Add an isset condition around your statement such as

{{if isset($Pricing->Commission)}} ...

Note that Commission must be a public property. Otherwise you should do as stated in the comment and add a check inside the getCommission method

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download