EJS包含模板,提供'`include`需要'文件名' 。选项'即使传递文件名也会出错。

时间:2015-06-21 23:13:52

标签: html node.js partials

我按照不同问题的建议传递了完整的文件名,但我仍然收到此错误:

`include` requires the 'filename' option.

这是我的app.coffee

db = require('./db')

http = require('http')
fs = require('fs')
ejs = require('ejs')
# bs = require('bootstrap')

db.albums.find({}, {}, 
    (err, data) ->
        if err?
            console.log("Error: failure to retrieve albums.")
            return
        else
            initiate_server(data)
            return
    ).sort({'play_counts': -1}).limit(20)

initiate_server = (albums) ->
    http.createServer(
        (req, res) ->
            res.writeHead(200, {'Content-Type': 'text/html'})
            # res.end(String(data))

            fs.readFile('views/index.ejs', 'utf-8',
                (err, data) ->
                    if err?
                        res.end('error occured')
                    else
                        dir_path = __dirname + '/views'

                        renderedHtml = ejs.render(data, 
                                {
                                    albums: albums,
                                    headerText: 'TempText',
                                    head: '/partials/head.ejs',
                                    header: dir_path + '/partials/header.ejs',
                                    footer: dir_path + '/partials/footer.ejs'
                                }
                            )
                        res.end(renderedHtml)

                        # res.end(data)
            )
            return
    ).listen(8080)
    console.log ('Server listening')
    return

这是我的index.ejs:

<!DOCTYPE html>
<html lang="en">
<head>
    <% include(head) %>
</head>

<body class="container">
    <header>
    </header>

    <main>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th> Rank </th>
                    <th> Album </th>
                </tr>
            </thead>
            <tbody>
              <% albums.forEach(function(album, count){ %>
                <tr>
                    <td><%= count %></td>
                    <td><%= album.album_name %></td>
                </tr>
              <% }) %>
            </tbody>
        </table>
    </main>

    <footer>
    </footer>

</body>
</html>

我也尝试过传递相对路径而且我还有这个错误,有什么建议吗?谢谢。

0 个答案:

没有答案