[英]How can I use Underscore.js templates in conjunction with EJS?
They both use the same syntax for inserting variables. 它们都使用相同的语法来插入变量。 For example if I want the following
例如,如果我想要以下内容
<%= username %>
In my Underscore, my main EJS breaks because it tries to replace username and no such variable exists in the main page. 在我的下划线中,我的主要EJS中断,因为它试图替换用户名,并且主页中不存在这样的变量。
I had this issue and thought I would share the solution I found for solving the issue client side. 我有这个问题,并认为我会分享我找到的解决客户端问题的解决方案。 Here is how your change the escape regex (via underscore.js docs ):
以下是您更改escape regex的方法(通过underscore.js 文档 ):
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g
};
var template = _.template( "{{example_value}}");
Changes the <%= %> to {{ }}. 将<%=%>更改为{{}}。
I think square brackets will work in EJS by default: 我认为方括号默认在EJS中起作用:
[%= username %]
And if you need to get fancier, the EJS github page describes how to create custom tags: 如果你需要变得更加漂亮,EJS github页面描述了如何创建自定义标签:
var ejs = require('ejs');
ejs.open = '{{';
ejs.close = '}}';
https://github.com/visionmedia/ejs https://github.com/visionmedia/ejs
Using the client side GitHub example, you'd need to do syntax like this when you render: 使用客户端GitHub示例,渲染时需要执行以下语法:
var html = require('ejs').render(users, { open: "^%", close: "%^" });
Options are the 2nd parameter of the render()
. 选项是
render()
的第二个参数。
I had the same issue when I wanted to render the webpage using ejs template on back-end (express), meanwhile I had to use underscore template on front-end. 当我想在后端(快速)使用ejs模板渲染网页时,我遇到了同样的问题,同时我不得不在前端使用下划线模板。
I tried Marc's answer but it doesn't help, I think it has been out of date to use in newer version. 我试过Marc的答案,但它没有帮助,我认为在新版本中使用它已经过时了。 In newer version of ejs(mine is
2.3.3
), you can no longer use ejs.open
and ejs.close
, use ejs.delimiter
instead. 在较新版本的ejs(我的是
2.3.3
)中,你不能再使用ejs.open
和ejs.close
,而是使用ejs.delimiter
。
I changed the delimiter to '$' in ejs so ejs would only handle with <$ $>
tag to insert variables and take <% %>
tag as meaningless syntax. 我在ejs中将分隔符更改为“$”,因此ejs只会使用
<$ $>
标记来处理变量并将<% %>
标记作为无意义的语法。
app.set('view engine', 'ejs');
var ejs = require('ejs');
ejs.delimiter = '$';
app.engine('ejs', ejs.renderFile);
NOTE: I add the code above in app.js
file in express applications and it worked fine, and if you want to use it on front-end, just pass {'delimiter': '$'}
in ejs.render(str, options)
as options argument. 注意:我在
app.js
文件中在快速应用程序中添加上面的代码并且工作正常,如果你想在前端使用它,只需在ejs.render(str, options)
传递{'delimiter': '$'}
ejs.render(str, options)
作为选项参数。
I recently ran into this issue and I didn't want to reconfigure EJS, so I changed how underscore interpolates, evaluates, and escapes values. 我最近遇到了这个问题而且我不想重新配置EJS,所以我改变了下划线插值,计算和转义值的方式。
By default, here are the current underscore templating settings: 默认情况下,以下是当前下划线模板设置:
_.templateSettings = {
interpolate: /<%([\s\S]+?)%>/g,
evaluate: /<%=([\s\S]+?)%>/g,
escape: /<%-([\s\S]+?)%>/g
};
Then I updated the settings to: 然后我将设置更新为:
_.templateSettings = {
interpolate: /\{\{=([^}]*)\}\}/g,
evaluate: /\{\{(?!=)(.*?)\}\}/g,
escape: /\{\{-([^}]*)\}\}/g
};
In other words, the snippet above will change the following in underscore: 换句话说,上面的代码段将在下划线中更改以下内容:
Interpolate 插
<%= ... %>
<%= ... %>
{{= ... }}
{{= ... }}
/\\{\\{=([^}]*)\\}\\}/g
/\\{\\{=([^}]*)\\}\\}/g
Evaluate 评估
<% ... %>
<% ... %>
{{ ... }}
{{ ... }}
/\\{\\{(?!=)(.*?)\\}\\}/g
/\\{\\{(?!=)(.*?)\\}\\}/g
Escape 逃逸
<%- ... %>
<%- ... %>
{{- ... }}
{{- ... }}
/\\{\\{-([^}]*)\\}\\}/g
/\\{\\{-([^}]*)\\}\\}/g
Then my underscore template looks like this: 然后我的下划线模板看起来像这样:
// Underscore
<script type="text/template">
<ul>
{{ _.each(collection, function(model) { }}
<li>{{= model.text }}</li>
<li>{{- model.textEscaped }}</li>
{{ }); }}
</ul>
</script>
...and my EJS templates remain the same and I can continue to use the default ERB syntax to interpolate/evaluates values: <% ... %>
and <%= ... %>
: ...我的EJS模板保持不变,我可以继续使用默认的ERB语法来插值/评估值:
<% ... %>
和<%= ... %>
:
// EJS
<% if (isAuthenticated) { %>
<%= user.displayName %>
<% } %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.