This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

tristanbailey
tristanbailey

Hi

I was trying to work out how to best iterate a folder and with help of this forum worked out a way so thought I would share it for others (as I am long time developer but newish to OctoberCMS)

  use Cms\Classes\Theme;
  function onStart()
  {
        $items     = [ ];
        // media dir
        $files     = File::allFiles( 'storage/app/media/gallery/' );
        // OR from theme dir
        // $theme_dir = Theme::getActiveTheme()->getDirName();
        // $files     = File::allFiles( themes_path( $theme_dir ) . '/assets/images/gallery/' );

        foreach ($files as $item) {
            $items[] = $item->getFilename();
        }

        rsort($items, SORT_NUMERIC); // Bigest file number first
        // $items = array_slice($items,0,20); // if you want a limit

        $this['gallery'] = $items;
        //dd( $items );
  }
  <h2>Gallery</h2>

    <div class="showcase-list">
        <div class="row">   
            {% for image in gallery %}
            <div class="col-xs-6 col-sm-3">
                <img src="{{ url('/') }}/storage/app/media/gallery/{{image}}">
            </div>
            {% endfor %}
        </div>
    </div>

A little easier when you dont need a plugin, for something simple.

Let me know if anything could improve here, as still learning the syntax.

info53680
info53680

Been trying to work this out all day and found this and help out alot!!!! Thank you!!!

tristanbailey
tristanbailey

Glad could help @info53680 it took me a few goes to work it out.

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.