0

i want to show folders that i ahve aunder some directory, im using express3-handlebars. html code:

{{gallery}}

here what i have:

res.render('pages/gallery', {
        title: 'גלריה',
        HomeActive:'',
        ContactActive:'',
        AboutActive:'',
        GalleryActive:'active',
        MapActive:'',
        EmailActive:'',
        helpers:{
            gallery:function(){
                var albums = [],
                    albumDir = './public/img/albums';
                eventEmitter.on('readFolder',function(){
                    var albums = [];
                    fs.readdir(albumDir, function (err, files) {
                        if (err) console.log(err);

                        for (var i = 0; i < files.length; i++) {

                            if (files[i].indexOf('.') == -1)
                                albums.push({album: files[i]});
                        }
                        console.log("read " + files);
                        eventEmitter.emit('folderRead',files);
                    });
                });
                console.log(albums);
                eventEmitter.emit('readFolder');
                return eventEmitter.on('folderRead',function(files){
                    console.log("end " + files)
                    return files;
                });
            }
        },
        PagitionHide:'hide'
    });

i had a problem to set albums without the eventEmitters, its seems like, doing:

gallery:function(){
    var albums = [],
            albumDir = './public/img/albums';

            fs.readdir(albumDir, function (err, files) {
                if (err) console.log(err);

                for (var i = 0; i < files.length; i++) {

                    if (files[i].indexOf('.') == -1)
                        albums.push({album: files[i]});
                }

                console.log(albums); //logs the folders names.
            });
console.log(albums); //logs an empty array
return albums;
}

cause albums to be empty, i guess beacsue of the async of node.js.

but now when the HTML is render its showing and object like so: [object Object] what am i doing wrong here?

Boaz Hoch
  • 1,219
  • 1
  • 12
  • 36

1 Answers1

0

Did you try this ?

{{#each gallery}}
    {{album}}
{{/each}}
Akaryatrh
  • 532
  • 2
  • 10