【问题标题】:In Jade how is this interpreted ? <%= lastName %>在 Jade 中这是如何解释的? <%= 姓氏 %>
【发布时间】:2013-07-10 18:19:58
【问题描述】:

我目前有这样的代码,但名字的显示方式是这样的。我正试图弄清楚这在玉中是什么,因为这是不对的。

玉文件

div.centerContent
    script(type="text/javascript", src="/js/main.js")

    h4 User goes here with equal before it no space
        div#user
            p!= "<%=firstName%>"
            | <%=lastName%>
            p!="<%= email %>"
            p <%=phone%>
            p <%=birthday%>
            button.edit Edit



    script(id="userTemplate", type ="text/template")
            p <%=firstName%> <%=lastName%>
            p <%=email%>
            p <%=phone%>
            p <%=birthday1%>
            button.edit Edit

    script(id="userEditTemplate", type ="text/template")
        div
            form(action="#")
                input(type="text", class="firstName", value="<%= firstName %>") input(type="text", class="lastName", value="<%= lastName %>")
                input(type="email", class="email", value="<%= email %>")
                input(type="date", class="birthday", value="<%= birthday %>")
            button.save Save
            button.cancel Cancel
    hr

在这里我将包含我的 main.js 文件,也许我在那里做错了什么。

main.js

(function () {
    window.App = {
        Models: {},
        Collections: {},
        Views: {},
        Templates: {},
        Router: {}

    };

    // MODEL
    App.Models.User = Backbone.Model.extend({
        defaults: {
            firstName: 'first',
            lastName: 'last',
            email: 'Email',
            phone: '222',
            birthday: 'date'
        },

        validate: function (attrs) {
            if (!attrs.firstName) {
                return 'You must enter a real first name.';
            }
            if (!attrs.lastName) {
                return 'You must enter a real last name.';
            }
            if (attrs.email.length < 5) {
                return 'You must enter a real email.';
            }
            if (attrs.phone.length < 10 && attrs.phone === int) {
                return 'You must enter a real phone number, if you did please remove the dash and spaces.';
            }
            if (attrs.city.length < 2) {
                return 'You must enter a real city.';
            }
        },

        initialize: function() {
             user.on('invalid', function (model, invalid) {
                console.log(invalid);
            });
        }

    });



    //VIEW
    App.Views.User = Backbone.View.extend({
        model: App.Models.User,
        //tagName: 'div',
        //id: 'user',
        //className: 'userProfile',
        template: _.template($("#userTemplate").html()),
        editTemplate: _.template($("#userEditTemplate").html()),

        initialize: function (){

        }

        render: function() {
            this.$el.html(this.template(this.model.toJSON()));
            return this;
        },

        events: {
            'click button.edit': 'editProfile',
        //  'click button.save': 'saveEdits',
            'click button.cancel': 'cancelEdits'
        },

        editProfile: function () {
            this.$el.html(this.editTemplate(this.model.toJSON()));

        }, 


        cancelEdits: function() {
            this.render();
        }

    });
    //start history service
    Backbone.history.start();

    var user = new App.Views.User({el: 'div #user'});
    user.render();
})();

【问题讨论】:

    标签: javascript node.js backbone.js pug


    【解决方案1】:

    在 Jade 看来,"&lt;%= firstName %&gt;" 只是一个 String 文字。但是,它将对其进行 HTML 编码:

    <input type="text" class="firstName" value="&lt;%= firstName %&gt;">
    

    要保持原样,请在= 之前添加! 以跳过编码。

    input(type="text", class="firstname", value!="<%= firstName %>")
    
    <input type="text" class="firstName" value="<%= firstName %>">
    

    来自documenation

    为安全起见,= 缓冲的代码默认被转义,但是要输出未转义的返回值,您可以使用!=

    p!= aVarContainingMoreHTML
    

    另请注意,if you're using an older version of Jadescript 元素的内容可能被整体视为文本文字。

    Jade 版本 0.31.0 已弃用隐式文本,仅支持脚本和样式。要解决此问题,您只需在脚本或样式标记后添加一个. 字符。

    在 0.31.0 之前,您的视图将呈现为 (abridged):

    <script id="userEditTemplate" type="text/template">
        div.userProfile
            form(action="#")
            # ...
    </script>
    

    【讨论】:

    • 对不起,如果我把它扔到一个 P 标签中,像这样 p
    • @Lion789 应该像这样工作,Jade 将其视为inline HTML。但是,您也可以使用 !=String 文字,例如属性:p!= "&lt;%= firstName %&gt;"
    • 是的,有翡翠 0.32.0,但仍然无法正常工作,只是将整个内容显示为字符串
    • 那里我更新了我的代码以包含js文件和整个jade文件,可能其他地方有错误
    • @Lion789 Backbone/Underscore 的模板语法&lt;%= ... %&gt;,只适合模板&lt;script&gt;s。如果要使用 Jade 输出值,例如 div#user 内部的初始显示,请使用其 interpolation syntaxp #{firstName} #{lastName}
    猜你喜欢
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2016-02-22
    • 1970-01-01
    相关资源
    最近更新 更多