我如何逃脱的Node.js EJS模板code在客户端进行评估? [英] How do I escape EJS template code in node.js to be evaluated on the client side?
问题描述
我用在服务器端的node.js / EJS和Backbone.js的客户端上。
包括服务器端和客户端使用相同的模板风格。
所以,问题是,如果我把模板code意味着一个模板内的客户仍然得到的解析服务器端。
I use node.js/ejs on the server side and backbone.js on the client side. Both server side and client side use the same templating style. So the problem is, if I put template code meant for the client inside a template it still get's parsed on the server side.
如果发现了这样的作品:
If found out that something like this works:
<%- "<%= done ? 'done' : '' %\>" %>
不过,恕我直言,这uglifies的code的方式,这使得使用模板无用的整点。
However, IMHO this uglifies the code in a way which makes the whole point of using templates useless.
你会如何处理这个?
有没有一种方法来定义里面EJS模板code不得到解析像一个{}文字块在其他模板语言-Tag使用?
Is there a way to define blocks of code inside EJS-templates which do not get parsed like a {literal}-tag used in other templating languages?
更新:现在我用的骨干_.templateSettings在客户端使用不同的分隔符
Update: For now I use backbone's _.templateSettings to use different delimiters on the client side.
更新:下面是在JSP环境类似的解决方案:下划线.js文件模板中的JSP
Update: Here's a similar solution in a JSP context: Underscore.js Templates Within JSP
推荐答案
我处理这个问题的方法是重写开始和结束标记的节点,以便EJS的2实例lookgin不同的标签。
The way I have dealt with this is to override the opening and closing tags on node so that the 2 instances of ejs are lookgin for different tags.
在节点,可以在选项通过
On node you can pass in options
{open:'<%',close:'%>'}
在我来说,我使用&lt;%和&lt; @我的两个版本。然后在节点EJS模板我有这样的事情(其中名称为骨干,从节点显然everyauth):
In my case I use <% and <@ for my two versions. Then in node ejs template I have something like this (where name is from backbone and everyauth obviously from node):
<% if(everyauth.loggedIn) %><h1><@= name @></h1><% } %>
这篇关于我如何逃脱的Node.js EJS模板code在客户端进行评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!