Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Calling custom functions from inside {if} or {assign}

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
jazzbazzfazz
Smarty n00b


Joined: 08 Oct 2003
Posts: 3

PostPosted: Wed Oct 08, 2003 11:25 pm    Post subject: Calling custom functions from inside {if} or {assign} Reply with quote

I searched and searched (and tried quite a few things)...

Suppose I want to compare the results of a custom function inside an {if} construction, or assign the result of a function to a new variable...it looks like the way Smarty requires parameters to be listed precludes using functions in such a way.

e.g.

Suppose the function "housevalue" is written to estimate the value of a home, given such parameters as square footage and number of bedrooms

{housevalue sqft=2000 bedrooms=3} might return something like 150000

I use smarty->register_function() to register this function in php/Smarty, then can call it from a Smarty template. Now, what I want to do is use this function in an {if} statement, or in an {assign} statement.

{if housevalue sqft=2000 bedrooms=3 gt 100000} doesn't work...

neither do lots of other wild potshots, e.g.

{if housevalue(2000,3) gt 100000}

...for this latter one I tried using array_values($params), e.g., instead of extract($params).

Any ideas?


Last edited by jazzbazzfazz on Wed May 05, 2010 10:52 pm; edited 1 time in total
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Oct 09, 2003 12:18 am    Post subject: Reply with quote

This is how other functions assign values:

{housevalue sqft=2000 bedrooms=3 assign="hval"}

Make sure your housevalue function assigns the result, then you can work with {$hval} from there. I'll also point out (as I always do Smile ) that this example of logic should ideally be carried out in the application, not the template.

Monte
Back to top
View user's profile Send private message Visit poster's website
jazzbazzfazz
Smarty n00b


Joined: 08 Oct 2003
Posts: 3

PostPosted: Thu Oct 09, 2003 4:23 pm    Post subject: Reply with quote

Well, the assign construct doesn't seem to work, either.

This is my test "testfunc.tpl" file:
=====================
<html>
<head><title>Testfunc</title></head>
<body>
<hr>
{testfunc echo=0 assign="x"}<br>
RESULT: x = [{$x}]
<hr>
{testfunc echo=1 assign="x"}<br>
RESULT: x = [{$x}]
<hr>
</body>
</html>
=====================

This is (an excerpt from) my test .php file:

=====================
function testfunc($params) {
extract($params);
if ($echo == 1) $result = "Echo!";
else $result = "No echo";
return $result;
}
=====================

Of course, I include this in the file also:

$smarty->register_function('testfunc','testfunc');

...

The result looks like this:

______________________________

No echo
RESULT: x = []
______________________________

Echo!
RESULT: x = []
______________________________

Any more suggestions? Thanks for your help!


Last edited by jazzbazzfazz on Wed May 05, 2010 10:52 pm; edited 1 time in total
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Oct 09, 2003 4:39 pm    Post subject: Reply with quote

ahem, you have to actually call

$smarty->assign($params['assign'], $result);

in your plugin-function
Back to top
View user's profile Send private message Send e-mail Visit poster's website
jazzbazzfazz
Smarty n00b


Joined: 08 Oct 2003
Posts: 3

PostPosted: Thu Oct 09, 2003 5:21 pm    Post subject: Problem solved! Reply with quote

Finally clued in to a solution...looked at the plugin directory and rewrote the function as follows:

==================
function testfunc($params, &$smarty) {
extract($params);
if ($echo == 1) $result = "Echo!";
else $result = "No echo";
if (!empty($params['assign'])) {
$smarty->assign($params['assign'], $result);
} else {
return $result;
}
}
==================

On first reading of the docs, it wasn't clear that user-defined/ user-registered functions (i.e. non plug-ins) have the same construction as plug-ins. In retrospect, that makes complete sense and I should have guessed...

Works like a champ! Thanks for all who scratched their heads on this one...hope this helps.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP