This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
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
Try $this['files'] = glob(themes_path('assets/images/subfolder//*.{jpeg,gif,png}'), GLOB_BRACE);
You can't use twig braces {{}}
inside the php header.
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
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 );
}
Thank you buddy. With tiny modifications like adding
use Cms\Classes\Theme;to the top of the code, it worked. Much appreciated!
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