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

Assign Insert results to a variable

 
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
fame
Smarty Rookie


Joined: 07 Oct 2003
Posts: 6

PostPosted: Tue Nov 11, 2003 6:01 pm    Post subject: Assign Insert results to a variable Reply with quote

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
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Nov 11, 2003 6:50 pm    Post subject: Reply with quote

hmmm. Perhaps put it in a capture block.
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Tue Nov 11, 2003 9:10 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
messju
Administrator


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

PostPosted: Tue Nov 11, 2003 11:22 pm    Post subject: Reply with quote

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) Sad

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
View user's profile Send private message Send e-mail Visit poster's website
fame
Smarty Rookie


Joined: 07 Oct 2003
Posts: 6

PostPosted: Wed Nov 12, 2003 12:06 am    Post subject: Reply with quote

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... Very Happy

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... Wink Thanks for the info. Let me know if you have any other help in the meantime.


jf
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Wed Nov 12, 2003 12:56 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
fame
Smarty Rookie


Joined: 07 Oct 2003
Posts: 6

PostPosted: Wed Nov 12, 2003 1:46 pm    Post subject: Reply with quote

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! Wink

jf
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