带有主干轨道的 Rails:EJS 文件中的资产助手 (image_path)

发布于 2024-12-18 15:44:58 字数 549 浏览 1 评论 0原文

我有一个使用 codebrew/backbone-rails 的 Rails 3.1 应用程序。在 .jst.ejs 模板中,我想包含一个图像,如下所示:

<img src="<%= image_path("foo.png") %>"/>

但是,当然资产助手在 JavaScript 中不可用。

链接 ERB (.jst.ejs.erb) 不起作用,因为 EJS 语法与 ERB 冲突。

这是我所知道的:

  • 资产助手在浏览器中不可用,因此我需要在服务器端运行它们。
  • 我可以通过让服务器将各种资源路径转储到 HTML 中(通过数据属性或

有没有办法以某种方式使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

绿萝 2024-12-25 15:44:58

实际上,有一种方法可以链接 .jst.ejs.erb 文件,尽管它没有文档记录,而且我只是通过查看 EJS 测试用例才找到它。您可以告诉 EJS 使用 {{ }} (或 [% %] 或您想要的任何其他内容)而不是 <% %>,然后 ERB 将不会尝试评估您的 EJS 调用。

确保在代码中的某个位置需要 EJS(我刚刚在 Gemfile 中包含了 gem 'ejs'),然后创建一个包含以下内容的初始值设定项(我将其称为 ejs.rb)

EJS.evaluation_pattern    = /\{\{([\s\S]+?)\}\}/
EJS.interpolation_pattern = /\{\{=([\s\S]+?)\}\}/

:确保将您的模板重命名为 .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:

EJS.evaluation_pattern    = /\{\{([\s\S]+?)\}\}/
EJS.interpolation_pattern = /\{\{=([\s\S]+?)\}\}/

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.

檐上三寸雪 2024-12-25 15:44:58

我可以看到两种方式。两者都不是伟大的。

当您说<%%=variable%>时,ERB会将其呈现为<%=variable%>,因此您可以将所有内容加倍转义< em>但是 asset_tags 以及它将在前往 EJS 的途中通过一次 ERB 传递而幸存下来。

如果您发现这太恶心了...

如何制作一个不同的 javascript 文件,带有 ERB 扩展名,来定义您的资源路径?然后使用资产管道来要求这一点。

所以说 assets.js.erb 定义了类似的内容:

MyAssets = {
  'foo': <%= image_path("foo.png") %>,
  ...
}

然后在清单顶部附近的某个地方需要它。然后引用在 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:

MyAssets = {
  'foo': <%= image_path("foo.png") %>,
  ...
}

And then require this somewhere near the top of your manifest. And then reference the globals however that works in EJS.

恰似旧人归 2024-12-25 15:44:58

对于那些愿意尝试 HAML 而不是 EJS 的人:通过 haml-coffee 使用 haml-coffee。 com/netzpirat/haml_coffee_assets">haml_coffee_assets 对我来说也很有效。

您可以在 .hamlc.erb 文件中包含以下内容:(

%img(src="<%= image_path('foo.png') %>")

但它仍然不提供路由帮助程序,仅提供资产帮助程序。)

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:

%img(src="<%= image_path('foo.png') %>")

(It still doesn't give you routing helpers though, only asset helpers.)

樱花落人离去 2024-12-25 15:44:58

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

清泪尽 2024-12-25 15:44:58

您可以通过以下 gem 使用相应的 Javascript 帮助器:
https://github.com/kavkaz/js_assets

最后(安装和配置后)您将能够像这样使用它:

<img src="<%= asset_path("foo.png") %>"/>

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:

<img src="<%= asset_path("foo.png") %>"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文