【发布时间】:2014-06-29 21:22:03
【问题描述】:
我正在使用 Smarty 开发我的最新项目,以使后端和前端更易于使用。
现在我在我的 webapp 中使用目录迭代函数来查看现有文件夹。
我希望有人可以为我“修复”此代码,也可以与 Smarty 一起正常工作。
我不能使用任何回声,而且我想返回输出的方式也有点乱。
希望任何人都可以帮助我,或者知道任何对我有用的提示......这是我第一次与 Smarty 合作
function requestAccountFolderStructure($dir) {
echo '<ul class="list-folderstructure">';
$path = $dir;
foreach (new DirectoryIterator($path) as $file) {
if ($file->isDot())
continue;
if ($file->isDir()) {
echo '<li class="li-folderstructure" data-folderstructure="' . $file->getFilename() . '">';
echo '<a class="a-folderstructure"><span class="name-folderstructure">' . $file->getFilename() . '</span> <span class="glyphicon glyphicon-play"></span></a>';
if (is_dir($dir . '/' . $file)) {
requestAccountFolderStructure($dir . '/' . $file);
}
echo '</li>';
}
}
echo '</ul>';
}
【问题讨论】:
-
问题是您应该在这样的功能中只准备数据,Smarty 应该简单地显示它们。您不应该混合显示数据和浏览目录结构的功能。在函数中准备数据并返回准备好的数组,将其分配给 smarty 变量,然后使用 try 显示它。也可以看stackoverflow.com/questions/437862/…
标签: php function directory iteration smarty