|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
jorcom Smarty n00b
Joined: 11 Dec 2014 Posts: 3
|
Posted: Thu Dec 11, 2014 11:33 am Post subject: own plugin doesn't work fine with paths |
|
|
Hi Everyone,
i'm working with smarty by first time and i need your help.
I created my own smarty plugin, this plugin only gets one param, with a path. Then, checks if there are any image (with .jpg extension) inside this path, and put all images found into another array.
Then, assign the second array into smarty, and i want it to use in a .tpl file, but when i do a foreach in tpl, the plugin works fine, but is checking a sibling folder, not the folder that set into a param when call the function.
I paste my code below to help you understand.
---- Smarty Plugin -----
-------------------------------------------------------------------------------
Code: | function smarty_function_getImages($params, &$smarty){
if(!isset($params['directory'])) {
trigger_error("Error, path is needed.", E_USER_NOTICE);
return;
}else{
if(is_dir($params['directory'])){
$aux = scandir($params['directory']);
$aux2 = array();
for($i = 0; $i<sizeof($aux); $i++){
if(!is_dir($aux[$i])){
if(substr($aux[$i], -3) == "jpg"){
echo $aux[$i];
array_push($aux2, $aux[$i]);
}
}
}
$smarty->assign('rightItems',$aux2);
}else{
trigger_error("Invalid folder.", E_USER_NOTICE);
return "Invalid folder.";
}
}
return;
} |
-----------------------------------------------------------------------------------
---- .tpl file ----
-----------------------------------------------------------------------------------
Code: | {foreach from=$rightItems item=foo}
{$foo}<br />
{/foreach}
|
-----------------------------------------------------------------------------------
Someone has had this problem?
I'm sure that will be a little mistake, but by the moment i don't found the solution
Thanks in advance |
|
Back to top |
|
jorcom Smarty n00b
Joined: 11 Dec 2014 Posts: 3
|
Posted: Thu Dec 11, 2014 12:47 pm Post subject: |
|
|
I finally found the error...
another line was calling the same function with another path, the problem is that the line is commented with html tags <!-- and -->
smarty comments must be {* *}
well, this will not forget |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Dec 11, 2014 9:08 pm Post subject: |
|
|
Don't use for while iterating over an array.
Use foreach. It's faster.
Also there's no reason to &$smarty, as objects ALWAYS passed by reference. |
|
Back to top |
|
jorcom Smarty n00b
Joined: 11 Dec 2014 Posts: 3
|
Posted: Fri Dec 12, 2014 8:07 am Post subject: |
|
|
Hi,
thanks for the info, I applied the changes that you comment
Regards |
|
Back to top |
|
|
|
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
|