dqdpz60048 2014-05-12 13:24
浏览 41
已采纳

PHP / Smarty - 目录迭代功能

I'm working on my latest project with Smarty, to make back-end and front-end easier to use.
Now i use in my webapp an directory iteration function to view existing folders.
I was hoping someone could 'fix' this code for me too work it correctly with Smarty too.
I can't use any echo's and the way i wanted to return the output also works kinda messy.
Hope anyone can help me, or know any good tips for me... This is my first time working with 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>';
}

Documentation: http://www.smarty.net/

  • 写回答

1条回答 默认 最新

  • dongxinm279890 2014-05-12 19:59
    关注

    You could realize it for example that way:

    PHP file:

    function requestAccountFolderStructure($dir) {
        $list = array();
        $path = $dir;
        foreach (new DirectoryIterator($path) as $file) {
            if ($file->isDot())
                continue;
    
            if ($file->isDir()) {
                $record = array();
                $record['name'] =  $file->getFilename();
                $record['sub'] = array();            
                if (is_dir($dir . '/' . $file)) {
                    $record['sub'] = requestAccountFolderStructure($dir . '/' . $file);
                }
                $list[] = $record;
            }
    
        }
        return $list;
    }
    
       $dir = 'YOUR DIR';
       $directories = requestAccountFolderStructure($dir);
       $smarty->assign('directories',$directories);
       $smarty->display('directory.tpl');
    

    Smarty file - method 1 using function:

     {function name=showDir}
    {if $directories|@count gt 0}
     <ul class="list-folderstructure">
      {foreach from=$directories item=item name=info}
            <li class="li-folderstructure" data-folderstructure="'{$item.name}} '">
                  <a class="a-folderstructure"><span class="name-folderstructure">{$item.name}</span> <span class="glyphicon glyphicon-play"></span></a>
    
                   {showDir directories=$item.sub}                             
    
             </li>
      {/foreach}
      </ul>
    
    
    {/if}
    {/function}
    
    {showDir directories=$directories}
    

    Smarty template - using include recursion

    {if $directories|@count gt 0}
    
    <ul class="list-folderstructure">
       {foreach from=$directories item=item name=info}
             <li class="li-folderstructure" data-folderstructure="'{$item.name}} '">
                  <a class="a-folderstructure"><span class="name-folderstructure">{$item.name}</span> <span class="glyphicon glyphicon-play"></span></a>
    
                        {include file="directory.tpl" directories=$item.sub}
    
             </li>
       {/foreach}
    </ul>
    
    {/if}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?