Rails with backbone-rails: asset helpers (image_path) in EJS files

后端 未结 5 517
无人共我
无人共我 2020-12-13 11:24

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:



        
5条回答
  •  抹茶落季
    2020-12-13 11:59

    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.

提交回复
热议问题