I have a smarty template that has a variable set that is a custom php class called “survey”. This object has a member variable called “recipientName”. When I print out the variable using the following code:
{$survey->recipientName}
it works fine. The recipient’s name gets printed out, as one would expect. However, if I try to pass that into a function, it does not seem to work right. Take the following code snippet:
{if substr($survey->recipientName,0,3) eq 'Bob'}
Hello Bob
{else}
You are not Bob
{/if}
This snippet will always print out “You are not Bob”, even if recipientName is set to “Bob Smith”. As a proof of concept, the following performs as expected:
{if substr("Bob Smith",0,3) eq 'Bob'}
Hello Bob
{else}
You are not Bob
{/if}
The above will print out “Hello Bob”.
Similarly, I have noticed weird behavior in other places as well. For example, take the following code snippet:
{assign var="recipname" value="$survey->recipientName"}
This will assign the variable recipname to a value of “->recipientName”. Am I missing something?
The substring issue was a non-issue, a simple positioning error.
The assign issue, however, was due to the quotes around the value. Changing it to {assign var=”recipname” value=$survey->recipientName} fixed it.