0

我想显示我在某个目录下的文件夹,我正在使用 express3-handlebars。html代码:

{{gallery}}

这是我所拥有的:

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'
    });

我在没有 eventEmitters 的情况下设置专辑时遇到了问题,它似乎在做:

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;
}

导致相册为空,我猜是由于 node.js 的异步。

但是现在当 HTML 呈现它的显示和对象时,如下所示:[object Object] 我在这里做错了什么?

4

1 回答 1

0

你试过这个吗?

{{#each gallery}}
    {{album}}
{{/each}}
于 2014-01-02T10:15:48.453 回答