quasiman Posted January 17, 2011 Share Posted January 17, 2011 I'm new to Smarty (this is v.2), so my question may seem dumb...or impossible. First, I have a link directory that I want to enhance a bit, to include a thumbnail from the 'Shrink the web' thumbnail service (shrinktheweb.com). So I wrote up a function that downloads/caches the thumbnail: function get_stw_image($url,$accesskey,$secretkey,$size = "sm") { $agents[] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"; $agents[] = "Opera/9.63 (Windows NT 6.0; U; ru) Presto/2.1.1"; $agents[] = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5"; $agents[] = "Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.8.1.18) Gecko/20081203 Firefox/2.0.0.18"; $agents[] = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16"; $agents[] = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1"; $local = $url; $fullpath = "/images/".$local; $expire_time = 5; //days before expired // Read file creation time $FileCreationTime = filectime($fullpath); // Calculate file age in seconds $FileAge = time() - $FileCreationTime; if(!file_exists($fullpath) || $FileAge > ($expire_time * 1440)) { $image = "http://images.shrinktheweb.com/xino.php?stwembed=1"; $image .= "&stwaccesskeyid=".$accesskey; $image .="&stwu=".$secretkey; $size .= "&stwsize=".$size; $image .= "&stwurl=".$url; $ch = curl_init($image); $fp = fopen($fullpath,'w'); $options = array( CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_HEADER => false, // don't return headers CURLOPT_FOLLOWLOCATION => false, // follow redirects CURLOPT_AUTOREFERER => true, // set referer on redirect CURLOPT_CONNECTTIMEOUT => 5, // timeout on connect CURLOPT_TIMEOUT => 5, // timeout on response CURLOPT_FILE => $fp, //file handler CURLOPT_MAXREDIRS => 0, // stop after 10 redirects CURLOPT_USERAGENT => $agents[rand(0,(count($agents)-1))] //random browser agents...probably not necessary ); curl_setopt_array( $ch, $options ); curl_exec($ch); } return $fullpath; } No big deal, the function works fine on its own. The problem now is that I can't figure out how to get it working in the template. The template assignment is just : $template->assign('links',$links); And then the html page loops through each from the database: <!--{section name=link loop=$links}--> ... <!--{$links[link].linkurl|escape:'htmlall'}--> ... <!--{/section}--> So...how is this supposed to work? How does my function know from the template which url is which? Can I write an assignment that would loop through at the same time? $template->assign('shrinktheweb',get_stw_image($linkurl,"MYACCESSKEY","MYSECRETKEY","sm")); Maybe my function needs to be changed to somehow reference the template? Any advise (or even direction to a similar scenario) would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/224679-smarty-templates-newbie-looping-through-images-with-function/ Share on other sites More sharing options...
quasiman Posted January 20, 2011 Author Share Posted January 20, 2011 I've partially figured this out, but still have an issue. template file: <!--{section name=link loop=$latest_links}--> <!--{assign var='linksurls' value=`$latest_links[link].linkurl`}--> <img src="<!--{$shrinktheweb|escape:'htmlall'}-->" /> index.php file: $template->display($templatefile); $linksurl = $template->get_template_vars('linksurls'); $template->assign('shrinktheweb',get_stw_image($linkurl,"MYACCESSKEY","MYSECRETKEY","sm")); While my image is downloaded correctly, this doesn't allow me use the 'shrinktheweb' template assignment because it's after the template execution. My image just comes out as <img src="" /> Quote Link to comment https://forums.phpfreaks.com/topic/224679-smarty-templates-newbie-looping-through-images-with-function/#findComment-1162616 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.