如何在 Jade 模板引擎中正确输出变量? [英] How to correctly output variables in Jade templating engine?

查看:49
本文介绍了如何在 Jade 模板引擎中正确输出变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了像 a(href="#{settings.url}") 这样的链接,但有人告诉我我可以做 a(href=settings.url)code>,这是一个更好的解决方案(虽然我不明白其中的区别).

I was using links like a(href="#{settings.url}") but someone told me that i could just do a(href=settings.url), which is a better solution (although I do not understand the difference).

但现在我有一个关于另一个用例的问题.我应该使用哪一个,如果有?为什么?

But now i have a question for another use case. Which one should i use, If any? And Why?

  1. link(rel="stylesheet", href="#{settings.url}/assets/css/main.css")

link(rel="stylesheet", href=settings.url + "/assets/css/main.css")

推荐答案

我想说没有很重要的区别,但让我们看看幕后:

I would say there is not a very important difference, but let's take a look behind the scenes:

第一个例子:

link(rel="stylesheet", href="#{locals.url}/assets/css/main.css")

有数据

{ url: 'www.example.com' }

生成此代码

function template(locals) {
    var buf = [];
    var jade_mixins = {};
    buf.push('<link rel="stylesheet"' + jade.attr("href", "" + locals.url + "/assets/css/main.css", true, false) + "/>");
    return buf.join("");
}

还有这个 HTML

<link rel="stylesheet" href="www.example.com/assets/css/main.css"/>

其次:

link(rel="stylesheet", href=locals.url + "/assets/css/main.css")

将(具有与上述相同的数据)产生

will (with the same data as above) produce

function template(locals) {
    var buf = [];
    var jade_mixins = {};
    buf.push('<link rel="stylesheet"' + jade.attr("href", locals.url + "/assets/css/main.css", true, false) + "/>");
    return buf.join("");
}

并生成 HTML(惊喜!):

and result in HTML (surprise!):

<link rel="stylesheet" href="www.example.com/assets/css/main.css"/>

经验教训:
您会看到两种方法"的差异很小(请参阅第一个示例中的 "" +).因此,请使用您最喜欢的任何东西.

Lesson learned:
You see the difference in both "methods" is marginal (see the "" + in the frist example). Ergo use whatever pleases you best.

这篇关于如何在 Jade 模板引擎中正确输出变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆