質問

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/

役に立ちましたか?

解決

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}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top