View previous topic :: View next topic |
Author |
Message |
fame Smarty Rookie
Joined: 07 Oct 2003 Posts: 6
|
Posted: Tue Nov 11, 2003 6:01 pm Post subject: Assign Insert results to a variable |
|
|
Hello,
I would like to return the results of an insert function to a smarty variable versus displaying the results on the page. Right now, I'm using:
Code: | {insert assign=tracks name="apf2" item_id=$product.productcode templates="3"} |
Smarty displays the results in where this code shows up and does not assign anything to $tracks.
Any examples or insight? Thanks!
jf |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Nov 11, 2003 6:50 pm Post subject: |
|
|
hmmm. Perhaps put it in a capture block. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Nov 11, 2003 9:10 pm Post subject: |
|
|
capture won't work. capture is not executed when the template is cached.
to me it looks like a bug. assign seems to be honored when caching is disabled, but ignored when caching is on (same with 2.5.0 and cvs-HEAD).
i will look into the issue and post an update when it's fixed.
for an intermediate fix, you could implement the assign itself in your insert-function. fortunately the assign-parameter is passed through to the insert-function. you can try putting something like
[php:1:faf88f275f]
if (isset($params['assign'])) {
$smarty->assign($params['assign'], $output);
} else {
return $output;
}
[/php:1:faf88f275f]
at the bottom of your insert-function.
HTH
messju |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Nov 11, 2003 11:22 pm Post subject: |
|
|
i committed a fix to cvs. thanks for pointing this out, fame!
my suggestion implementing it inside the function itself turned out to have some little loop-back-issue: it only works if the real assign does not work (and does not overwrite the assigned var with the empty return)
fame: overall i'm curious what you do with assigning insert's output. i cannot imagine a useful application for it and actually it seems it never worked and you where the first who stumbled over it. it'd be great if you can tell us.
greetings
messju |
|
Back to top |
|
fame Smarty Rookie
Joined: 07 Oct 2003 Posts: 6
|
Posted: Wed Nov 12, 2003 12:06 am Post subject: |
|
|
messju wrote: | fame: overall i'm curious what you do with assigning insert's output. i cannot imagine a useful application for it and actually it seems it never worked and you where the first who stumbled over it. it'd be great if you can tell us. |
So, it's not just me then...
I think the best way to explain the application is to show the insert function:
Code: | function insert_apf($apf_array) {
@include("http://cmmdb.com/include/CGI_SCRIPT_GOES_HERE.cgi");
} |
The result of that CGI script is basic html if the item I'm looking for is there. If not, then it returns nothing. In my template, I want to display a section ONLY IF that script returns a result. Hence, my wanting to assign the output to a variable for further IF...THEN testing in Smarty.
I have tried doing something like this in the insert function:
Code: | function insert_apf2($apf_array) {
global $smarty;
@$apf = include("http://cmmdb.com/include/CGI_SCRIPT_GOES_HERE.cgi");
$smarty->assign("tracks",$apf);
} |
But $tracks always equals 1 no matter if the content I'm looking for is there or not (obviously because the include completes successfully.
I hope that helps explain it... Thanks for the info. Let me know if you have any other help in the meantime.
jf |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Nov 12, 2003 12:56 am Post subject: |
|
|
if you want to assign the output of your include, not the result, see http://php.net/outcontrol . it's really easy and yet powerful.
but there is no merit in assigning that inside an insert-function. if your page is cached the results don't get printed but a cached {if} and cached results are printed. |
|
Back to top |
|
fame Smarty Rookie
Joined: 07 Oct 2003 Posts: 6
|
Posted: Wed Nov 12, 2003 1:46 pm Post subject: |
|
|
Thanks for the info... Here's the solution I came up with for now:
Code: | function insert_apf2($apf_array) {
global $smarty;
ob_start();
@include("http://cmmdb.com/include/CGI_SCRIPT.CGI");
$tracks = ob_get_contents();
$smarty->assign("tracks",$tracks);
ob_end_clean();
} |
Great tip. I do appreciate it!
jf |
|
Back to top |
|
|