-1

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/

Bentley
  • 153
  • 1
  • 9
  • The thing is you should in such function just prepare data and Smarty should simple display them. You should not mix in function displaying data and going through directory structure. Prepare your data in function and return prepared array, assign it to smarty variable and then using try to display it. You can also look at http://stackoverflow.com/questions/437862/what-is-the-best-way-to-handle-recursion-in-smarty – Marcin Nabiałek May 12 '14 at 14:22

1 Answers1

0

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}
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291