Class: Rake::Pipeline::Web::Filters::HandlebarsFilter
- Inherits:
-
Filter
- Object
- Filter
- Rake::Pipeline::Web::Filters::HandlebarsFilter
- Includes:
- FilterWithDependencies
- Defined in:
- lib/rake-pipeline-web-filters/handlebars_filter.rb
Overview
A filter that converts handlebars templates to javascript and stores them in a defined variable.
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
A hash of options for generate_output.
Instance Method Summary collapse
- #generate_output(inputs, output) ⇒ Object
-
#initialize(options = {}, &block) ⇒ HandlebarsFilter
constructor
A new instance of HandlebarsFilter.
Constructor Details
#initialize(options = {}, &block) ⇒ HandlebarsFilter
Returns a new instance of HandlebarsFilter.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rake-pipeline-web-filters/handlebars_filter.rb', line 31 def initialize(={},&block) # Convert .handlebars file extensions to .js block ||= proc { |input| input.sub(/\.handlebars|\.hbs$/, '.js') } super(&block) @options = { :target =>'Ember.TEMPLATES', :wrapper_proc => proc { |source| "Ember.Handlebars.compile(#{source});" }, :key_name_proc => proc { |input| File.basename(input.path, File.extname(input.path)) } }.merge() end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Returns a hash of options for generate_output.
19 20 21 |
# File 'lib/rake-pipeline-web-filters/handlebars_filter.rb', line 19 def @options end |
Instance Method Details
#generate_output(inputs, output) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rake-pipeline-web-filters/handlebars_filter.rb', line 42 def generate_output(inputs, output) inputs.each do |input| # The name of the template is the filename, sans extension name = [:key_name_proc].call(input) # Read the file and escape it so it's a valid JS string source = input.read.to_json # Write out a JS file, saved to target, wrapped in compiler output.write "#{[:target]}['#{name}']=#{[:wrapper_proc].call(source)}" end end |