kmurph79
kmurph79

Reputation: 1202

Compressing JST templates with Rails 3.2 and Asset Pipeline

I'm using rails-backbone and thus JST templates via the EJS gem (bundled by rails-backbone). This isn't a huge issue, but whitespace in the JST templates isn't removed by the JS compressor. So, the obvious question is: how do I enable the jst.ejs templates to be compressed by the asset pipeline?

Thank you for any help.

Upvotes: 0

Views: 695

Answers (1)

kmurph79
kmurph79

Reputation: 1202

My solution:

# initializers/clean_ejs_template.rb

require 'ejs'

module EJS
  class << self
    def compile(source, options = {})
      source = source.dup

      escape_quotes!(source)
      #replace_interpolation_tags!(source, options)
      #replace_evaluation_tags!(source, options)
      escape_whitespace!(source)

      # remove extra whitespace and newlines
      source.gsub!(/\s{2,}|\\n/,'')
      # use _.template instead
      "_.template('#{source}')"

      #"function(obj){var __p=[],print=function(){__p.push.apply(__p,arguments);};" +
      #  "with(obj||{}){__p.push('#{source}');}return __p.join('');}"
    end
  end
end

Upvotes: 1

Related Questions