Reputation: 57766
Is there any function in smarty to check valid object of class in smarty?
Suppose $obj
is having some value or not.
How to check $obj
is object of 'TestClass' or not in smarty?
Upvotes: 5
Views: 7440
Reputation: 339
here is good example for this.
{if is_object($obj)}
{*=== your code ===*}
{else}
{*=== your code ===*}
{/if}
we can use is_object to know that this is an object or not.
hope it will help someone..
Upvotes: 2
Reputation: 20091
You can get the specific class of the object as well if needed with $obj|get_class
Example:
{if $animal instanceof Horse}
<span>Yup, it's a horse class.</span>
{else}
<span>It is actually a {{$animal|get_class}}</span>
{/if}
Upvotes: 0
Reputation: 15464
Function is_a
could be used for this.
{if is_a($customer, 'Customer')}
YES, instance of Customer
{else}
NO, Not an instance
{/if}
Upvotes: 0
Reputation: 57766
This is the way to check variable is object of specific class in Smarty.
if( true eq isset($obj) && true eq is_object($obj) && $obj instanceof 'TestClass' ){
//do something
}
Upvotes: 6
Reputation: 356
you can call php functions in the smarty code. Try this:
{if $customer instanceof Customer}
YES, instance of Customer
{else}
NO, Not an instance
{/if}
Also, it might be good idea to check if the variable is actually set before using it if the controller code has many paths:
{if isset($customer) && $customer instanceof Customer}
YES, instance of Customer
{else}
NO, Not an instance
{/if}
Upvotes: 1
Reputation: 13557
this works in Smarty2 and Smarty3:
{if $obj instanceof TestClass}
…
{/if}
Upvotes: 6
Reputation: 4461
try this
if($obj instanceof TestClass )
{
echo 'yes';
}
else
{
echo 'no';
}
Upvotes: 3