带有主干轨道的 Rails:EJS 文件中的资产助手 (image_path)
我有一个使用 codebrew/backbone-rails 的 Rails 3.1 应用程序。在 .jst.ejs 模板中,我想包含一个图像,如下所示:
<img src="<%= image_path("foo.png") %>"/>
但是,当然资产助手在 JavaScript 中不可用。
链接 ERB (.jst.ejs.erb) 不起作用,因为 EJS 语法与 ERB 冲突。
这是我所知道的:
- 资产助手在浏览器中不可用,因此我需要在服务器端运行它们。
- 我可以通过让服务器将各种资源路径转储到 HTML 中(通过数据属性或
和 JSON)并在 JS 中读回它们来解决这个问题,但这看起来相当笨拙。
有没有办法以某种方式使用 EJS 文件中的资产助手?
I have a Rails 3.1 app that uses the codebrew/backbone-rails. In a .jst.ejs template, I would like to include an image, like so:
<img src="<%= image_path("foo.png") %>"/>
But of course the asset helpers are not available in JavaScript.
Chaining ERB (.jst.ejs.erb) does not work, because the EJS syntax conflicts with ERB.
Here is what I know:
- The asset helpers are not available in the browser, so I need to run them on the server side.
- I can work around the problem by making the server dump various asset paths into the HTML (through data attributes or
<script>
and JSON) and reading them back in JS, but this seems rather kludgy.
Is there a way to somehow use the asset helpers in EJS files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
实际上,有一种方法可以链接 .jst.ejs.erb 文件,尽管它没有文档记录,而且我只是通过查看 EJS 测试用例才找到它。您可以告诉 EJS 使用 {{ }} (或 [% %] 或您想要的任何其他内容)而不是 <% %>,然后 ERB 将不会尝试评估您的 EJS 调用。
确保在代码中的某个位置需要 EJS(我刚刚在 Gemfile 中包含了
gem 'ejs'
),然后创建一个包含以下内容的初始值设定项(我将其称为 ejs.rb):确保将您的模板重命名为 .jst.ejs.erb,并替换现有的 <% %> EJS 解释的代码带有 {{ }}。如果您想使用 {{ }} 以外的其他内容,请更改初始值设定项中的正则表达式。
我希望 Sprockets 中有一个选项可以通过配置来处理此问题,而不必显式包含 EJS,但目前据我所知,还没有办法做到这一点。
There is a way, actually, to chain a .jst.ejs.erb file, although it's fairly undocumented, and I only found it through looking at the EJS test cases. You can tell EJS to use {{ }} (or [% %] or whatever else you want) instead of <% %>, and then ERB won't try to evaluate your EJS calls.
Make sure to require EJS somewhere in your code (I just included
gem 'ejs'
in my Gemfile), and then create an initializer (I called it ejs.rb) that includes the following:Then just make sure to rename your templates to .jst.ejs.erb, and replace your existing <% %> EJS-interpreted code with {{ }}. If you want to use something other than {{ }}, change the regular expressions in the initializer.
I wish there were an option in Sprockets to handle this through the config rather than having to explicitly include EJS, but as of the moment, there's no way to do that that I know of.
我可以看到两种方式。两者都不是伟大的。
当您说
<%%=variable%>
时,ERB会将其呈现为<%=variable%>
,因此您可以将所有内容加倍转义< em>但是 asset_tags 以及它将在前往 EJS 的途中通过一次 ERB 传递而幸存下来。如果您发现这太恶心了...
如何制作一个不同的 javascript 文件,带有 ERB 扩展名,来定义您的资源路径?然后使用资产管道来要求这一点。
所以说
assets.js.erb
定义了类似的内容:然后在清单顶部附近的某个地方需要它。然后引用在 EJS 中有效的全局变量。
I can see two ways. Neither are great.
When you say
<%%= variable %>
then this is rendered by ERB as<%= variable %>
, so you could double percent escape everything but the asset_tags and that would survive the trip through one ERB pass on the way to EJS.If you find that too gross...
How about making a different javascript file, with an ERB extension, that defines your asset paths? And then use the asset pipeline to require that.
So say
assets.js.erb
defines something like:And then require this somewhere near the top of your manifest. And then reference the globals however that works in EJS.
对于那些愿意尝试 HAML 而不是 EJS 的人:通过 haml-coffee 使用 haml-coffee。 com/netzpirat/haml_coffee_assets">haml_coffee_assets 对我来说也很有效。
您可以在 .hamlc.erb 文件中包含以下内容:(
但它仍然不提供路由帮助程序,仅提供资产帮助程序。)
For those willing to try HAML instead of EJS: Using haml-coffee through haml_coffee_assets has worked well for me as well.
You can have the following in a .hamlc.erb file:
(It still doesn't give you routing helpers though, only asset helpers.)
Ryan Fitzgerald 很友善地发布了他的 JavaScript 资产助手的要点(使用 ERB 预编译):https://gist .github.com/1406349
Ryan Fitzgerald was kind enough to post a gist of his JavaScript asset helpers (which get precompiled with ERB): https://gist.github.com/1406349
您可以通过以下 gem 使用相应的 Javascript 帮助器:
https://github.com/kavkaz/js_assets
最后(安装和配置后)您将能够像这样使用它:
You can use corresponding Javascript helper via the following gem:
https://github.com/kavkaz/js_assets
Finally (after installing and configuring) you will be able to use it like this: