我有一个函数在smarty检查是否行称为"mikaphotos"是空的。如果行是空的,没关系,不会发生任何事情,如果它不是空的,它在$path变量中取值。现在我的任务是:
$smarty->assign("mikaPhotos", getTheImages("SOMEPATH",400), true);
和我的getTheImages函数是:
getTheImages($path,$width){
$dirname = $path; //the path
$images = glob($dirname."*.png");//get the png images from directory
foreach($images as $image) {
echo '<img src="'.$dirname.'/'.$image.'" width="'.$width.'" /><br />';//echo all the images in SOMEPATH
}
}
现在在我的文件中。tpl . .我输入:{$mikaPhotos},它不工作。我认为有一些问题在我的代码,但我不知道如何做到这一点。请帮忙好吗?
函数smarty中的assign
将getTheImages
函数返回的值(!)赋值给mikaPhotos
变量。因此,您的getTheImages
函数应该返回一个字符串,而不是回显。例如:
getTheImages($path,$width){
$result = '';
$dirname = $path; //the path
$images = glob($dirname."*.png");//get the png images from directory
foreach($images as $image) {
$result .= '<img src="'.$dirname.'/'.$image.'" width="'.$width.'" /><br />';
}
return $result; // return html for all your images as a string
}
ok,谢谢大家,我得到了正确的代码:
function getTheImages($path,$width){
$result = '';
/*$dirname = "../uploads/reko/".$path.""; //the path
$images = glob($dirname."*.png");//get the png images from directory
foreach($images as $image){
$result .= '<img src="uploads/reko/'.$path.'/'.$image.'" width="'.$width.'" /><br />';
}*/
$handle = opendir("uploads/reko/".$path."");
while($file = readdir($handle)){
if($file !== '.' && $file !== '..'){
$result .= '<img src="uploads/reko/'.$path.'/'.$file.'" width="'.$width.'" /><br />';
}
}
return $result;
}