通过EJS模板传递变量

通过EJS模板传递变量

本文介绍了通过EJS模板传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的expressjs项目中使用Ejs模板引擎,尽管将对象传递到我的view blog.ejs文件中,但我仍然收到 blogpost未定义错误我的ejs文件。我的<%blogpost.forEach(function(blogpost){%> )行中发生错误。我认为这与即时消息传递对象和

I am using the Ejs templating engine for my expressjs project and despite passing my objects along to my view blog.ejs file, I am receiving an blogpost not defined error in my ejs file. Error is happening at my <% blogpost.forEach(function(blogpost) { %> line. I figure that is has something to do with how im passing the object and its properties, but I followed guides and it appears correct.

routes.js:

routes.js:

//blog
    router.route('/blog')

        // START POST method
        .post(function(req, res) {

            var blogpost = new Blogpost(); // create a new instance of a Blogpost model

            blogpost.title = req.body.title; // set the blog title
            blogpost.author = req.body.author; // set the author name
            blogpost.content = req.body.content; // set the blog content
            blogpost.date = req.body.date; // set the date of the post
                //Save Blog Post
                blogpost.save(function(err) {
                    if (err)
                        res.send(err);

                    res.json({ message: 'Blog created.' });
                });

        }) // END POST method


        // START GET method
        .get(function(req, res) {
            Blogpost.find(function(err, blogpost) {
                if (err)
                    res.send(err);

                blogpost.title = req.body.title; // update the blog title
                blogpost.author = req.body.author; // set the author name
                blogpost.content = req.body.content; // update the blog content
                blogpost.date = req.body.date; // set the date of the post

                res.render('pages/blog', {
                    title: blogpost.title,
                    author: blogpost.author,
                    content: blogpost.content,
                    date: blogpost.date
                });
            });
        }); // END GET method

blog.ejs:

<html>
<head>
    <% include ../partials/head %>
</head>

<body>

    <header>
        <% include ../partials/header %>
    </header>

    <div class="grid">
        <div class="col-1-1">
            <div class="body-content">
                <% blogpost.forEach(function(blogpost) { %>
                    <h1><%= blogpost.title %></h1>
                    <% }); %>
            </div>
        </div>

    </div>




    <footer>
        <% include ../partials/footer %>
    </footer>

</body>
</html>


推荐答案

您没有传递名为<$的数组变量c $ c> blogpost 到模板中,而是将这些变量传递到模板中:

You're not passing an array variable called blogpost to your template, you are instead passing these variables to your template:

title: blogpost.title,
author: blogpost.author,
content: blogpost.content,
date: blogpost.date

您可以执行此 render()而不是当前使用的操作:

You could just do this render() instead of the one you currently have:

res.render('pages/blog', {
  blogpost: blogpost,
});

这篇关于通过EJS模板传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 15:21