与骨干网护栏扶手:在EJS文件资产佣工(IMAGE_PATH) [英] Rails with backbone-rails: asset helpers (image_path) in EJS files

查看:142
本文介绍了与骨干网护栏扶手:在EJS文件资产佣工(IMAGE_PATH)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails 3.1的应用程序,使用 codebrew /骨干网护栏的。在.jst.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") %>"/>

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

But of course the asset helpers are not available in JavaScript.

链接ERB(.jst.ejs.erb)不起作用,因为与雇员再培训局的EJS语法冲突。

Chaining ERB (.jst.ejs.erb) does not work, because the EJS syntax conflicts with ERB.

下面是我知道的:


  • 资产助手都没有在浏览器中使用,所以我需要在服务器端运行它们。

  • 我可以通过使服务器转储各类资产路径到HTML解决问题(通过数据属性或&LT;脚本&GT; 和JSON),并在阅读他们回来JS,但这似乎相当缺憾。

  • 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.

有没有办法以某种方式使用EJS文件中的资产佣工?

Is there a way to somehow use the asset helpers in EJS files?

推荐答案

有一个办法,实际上,以链.jst.ejs.erb文件,虽然这是相当无证,我只发现它通过看EJS测试用例。你可以告诉EJS使用{{}}(或[%%]或任何其他你想要的)代替&LT;%%>,然后再培训局会不会尝试评估您的通话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.

确认要求EJS在code的地方(我只是包括创业板EJS在我的Gemfile),然后创建一个初始化(我把它叫做ejs.rb),包括以下内容:

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]+?)\}\}/

然后,只需确保重命名模板.jst.ejs.erb,并取代现有的&LT;%%> EJS-间preTED code与{{}}。如果你想使用比其他的东西{{}},改变常规的前pressions在初始化。

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.

我希望有链轮中的一个选项通过配置来处理这个,而不必明确包括EJS,但作为时刻,有没有办法做到这一点,我知道的。

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.

这篇关于与骨干网护栏扶手:在EJS文件资产佣工(IMAGE_PATH)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆