【问题标题】:How do I render the contents of a variable in an ejs template?如何在 ejs 模板中呈现变量的内容?
【发布时间】:2017-11-02 23:29:30
【问题描述】:

我有一个 ejs 模板,它应该在页面加载时通过调用用户的名字来问候用户,但是它不显示任何内容!这是我的模板:

<!DOCTYPE html>
<html>
    <head>
        <title>quotes</title>
        <link rel="icon" href="favicon.ico" type="image/x-icon">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">;
        <link rel="stylesheet" type="text/css" href="stylesheets/profile.css">
    </head>
    <body>
        <div class="wrapper">
            <h1 class="title">profile</h1>
            <nav class="nav-bar">
                <ul class="nav-list">
                    <li><a href="/">home</a></li>
                    <li><a href="/logout">logout</a></li>
                </ul>
            </nav>

            // not displaying user here
            <h1 class="greeting">Welcome <% user %></h1>
            <form action="/quotes" method="POST" class="new-entry" autocomplete="off">
                <h2 class="subtitle">Add a quote</h2>
                <input type="text" placeholder="quote" name="quote" pattern=".{3,}" required oninvalid="this.setCustomValidity('3 characters minimum')">

                <input type="text" placeholder="author" name="author" pattern=".{1,}" required oninvalid="this.setCustomValidity('1 characters minimum')">

                <button type="submit" class="red-button">Add</button>
            </form>
            <footer>
                &copy; 2017
            </footer>
        </div>
        <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>;
        <script src="javascripts/main.js"></script>
    </body>
</html>

【问题讨论】:

    标签: html express ejs


    【解决方案1】:

    您使用了无效的 ejs 语法。假设后端代码写得正确,你需要在开始的 ejs 标签上加一个等号,否则模板不知道读取变量的内容。

    <%= user %>
    

    应该给你你正在寻找的结果。

    您可以在此处找到有关正确 ejs 语法的更多信息:http://ejs.co/

    【讨论】:

    • 这解决了我的问题!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-01-29
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    相关资源
    最近更新 更多