Rails 3.1 中的 EJS gem 无法正确编译 JavaScript 模板
我正在尝试在 Rails 3.1 中使用 EJS gem 进行模板化。当我在 application.js 文件中需要我的模板时,
//= require_directory ./templates
我在客户端获得的输出将模板包装在匿名函数中并为其命名空间,但是......就是这样。这是我得到的生成输出。
(function() {
this.JST || (this.JST = {});
this.JST["templates/index"] = <article class="item <%=type%>">
<% if (type === "stat") { %>
<h2>
<span>70%</span>
of teens have one or more social network profiles
</h2>
<% } else { %>
<header>
<a href="/posts/<%=id%>">
<h3><%=type%></h3>
<h2><span>- <%=type%></span></h2>
</a>
</header>
<% if (confidential) { %>
<span class="confidential">Confidential</span>
<% } %>
<% if (type === "video" || type === "music") { %>
<a href="/posts/<%=id%>" class="play">play</a>
<% } %>
<a href="/posts/<%=id%>"><img src="<%=image%>" alt="" /></a>
<% } %>
</article>;
}).call(this);
我希望模板被编译成字符串。这就是我过去与 Jammit 的经历。我需要手动执行此操作吗?我错过了什么吗?
预先感谢,
A
I am trying to use the EJS gem for templating in rails 3.1. When I require my template in the application.js file
//= require_directory ./templates
The output I get on the client side wraps the template in an anonymous function and namespaces it, but... that's it. This is the generated output I get.
(function() {
this.JST || (this.JST = {});
this.JST["templates/index"] = <article class="item <%=type%>">
<% if (type === "stat") { %>
<h2>
<span>70%</span>
of teens have one or more social network profiles
</h2>
<% } else { %>
<header>
<a href="/posts/<%=id%>">
<h3><%=type%></h3>
<h2><span>- <%=type%></span></h2>
</a>
</header>
<% if (confidential) { %>
<span class="confidential">Confidential</span>
<% } %>
<% if (type === "video" || type === "music") { %>
<a href="/posts/<%=id%>" class="play">play</a>
<% } %>
<a href="/posts/<%=id%>"><img src="<%=image%>" alt="" /></a>
<% } %>
</article>;
}).call(this);
I would expect the template to be compiled into a string. That's the experience I've had with Jammit in the past. Do I need to do that manually? Am I missing something?
Thanks in advance,
A
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Sprockets 没有通过 EJS 处理您的模板,因为它没有以“ejs”结尾。您需要在模板文件中使用以“.jst.ejs”结尾的扩展名,以便以正确的顺序处理它们。
Sprockets wasn't processing your template through EJS because it didn't end in 'ejs'. You need to use an extension ending in ".jst.ejs" with your template files to get them processed in the right order.
嗯,
有趣的是,安装 Rails-backbone gem,而不是手动将骨干放置在应用程序中,似乎可以解决问题。我还将模板移至主干目录结构中。也许 ejs gem 对主干 gem 有一些依赖(我认为不太可能)?还是与目录嵌套级别或资产管道包含目录的方式有关?
不管怎样,不知道为什么这有效,但它仍然有效。如果有人能提供解释,我将不胜感激。
Hmm,
Interestingly, installing rails-backbone gem, rather than placing backbone in the app manually, seemed to solve the problem. I also moved the templates into the backbone directory structure. Maybe the ejs gem has some dependency on the backbone gem (unlikely I think)? Or is it something to do with directory nesting levels, or the way asset pipeline includes directories?
Either way, not sure why this is working but it is working none the less. If anyone could serve up an explanation, I'd appreciate it.