简体   繁体   English

Node.js Express和EJS将对象传递到模板

[英]Node.js express and EJS passing object to template

I'm passing object to my template and want to display object details in html. 我正在将对象传递给模板,并希望以html显示对象详细信息。

app.get('/', function (req, res) {
    var user = req.session.passport.user;

    if ( user != 'undefined' ){
        res.render('pages/chat.ejs', {
            user_data: user // get the user out of session and pass to template
        });
    } else {
        res.render('pages/chat.ejs', {
            user_data: false
        });
    }
});

and in my pages/chat.ejs: 在我的页面/chat.ejs中:

<% if (!user_data) { %>
            <div class="buttons pull-right clearfix">
                <span data-action="chatLogin" class="pull-right bordered button_blue button">Log in</span>
            </div>
            <% } else { %>
            <div class="buttons pull-right clearfix">
                <span data-action="addChatMessage" class="pull-right bordered button_red button">SEND</span>
                <span class="pull-right bordered button_blue button" id="user_name_details">
                    <% user_data.user_display_name %>
                </span>
            </div>
            <% } %>

but there is nothing in <% user_data.user_display_name %> so how to display object details in template? 但是<% user_data.user_display_name %>没有任何内容,那么如何在模板中显示对象详细信息?

Use <% = user_data.user_display_name %> 使用<% = user_data.user_display_name%>

<% %> is used for marking control flow.. It does not output any data <%%>用于标记控制流。它不输出任何数据

<%= %> is used to output the data of the enclosed variable in the template <%=%>用于输出模板中包含的变量的数据

<%- %> is used to output the unescaped value of the variable in the template so that characters like < and > don't get eaten by the HTML parser. <%-%>用于输出模板中变量的未转义值,以使<和>之类的字符不会被HTML解析器占用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM