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

Meysam
Meysam

I need to show all the images found under "/themes/theme_name/assets/images/subfolder/*". I thought maybe I could use the glob function in the code section of my page to load images:

function onStart()
{
    $this['files'] = glob('{{ 'assets/images/subfolder/'|theme }}/*.{jpeg,gif,png}', GLOB_BRACE);
}

But this syntax was not accepted. How can I achieve my goal?

Last updated

jwilson8767
jwilson8767

Try $this['files'] = glob(themes_path('assets/images/subfolder//*.{jpeg,gif,png}'), GLOB_BRACE);

You can't use twig braces {{}} inside the php header.

Meysam
Meysam

Are you sure "themes_path" works correctly? Where has this function been documented?

jwilson8767
Meysam
Meysam

Unfortunately, there is only the name of the function in that page and no description for that (click on the themes_path link, and nothing happens).

$this['files'] = glob(themes_path('assets/images/subfolder//*.{jpeg,gif,png}'), GLOB_BRACE); returns an empty array apparently. I also tried

$this['files'] = scandir(themes_path('assets/images/subfolder'));

The above line is producing the following exception:

scandir(E:\www\mywebsite/themes/assets/images/subfolder,E:\www\mywebsite/themes/assets/images/subfolder): The system cannot find the path specified. (code: 3)

As you can see, the problem is that the theme_name directory, has not been included in the path. What's wrong with my code?

Last updated

jwilson8767
jwilson8767

Huh, you're right. Alright then, I cobbled this together and verified it works. Hope you get good mileage out of it:


    public function onStart()
    {
        $items     = [ ];
        $theme_dir = Theme::getActiveTheme()->getDirName();
        $files     = File::allFiles( themes_path( $theme_dir ) );
        foreach (glob( themes_path( $theme_dir ) . '/*.{yaml,png}', GLOB_BRACE ) as $item) {
            $items[] = url( str_replace( themes_path(), '', $item ) );
        }
        dd( $items );
    }
Meysam
Meysam

Thank you buddy. With tiny modifications like adding

use Cms\Classes\Theme;
to the top of the code, it worked. Much appreciated!

laszlo.zenware.io
laszlo.zenware.io

I wanted to do something similar. These posts helped, thanks! I also wrote up a quick post about iterating over a folder and showing all images therein if anyone needs something similar.

1-8 of 8

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