feature: add jst template file have cache

This commit is contained in:
hysios 2012-02-08 21:54:34 +08:00
parent ed4590ee44
commit b6a64c5b1d
3 changed files with 42 additions and 19 deletions

View File

@ -24,6 +24,7 @@ module Jasmine
autoload :CoffeeTemplate, 'jasmine/headless/coffee_template'
autoload :JSTemplate, 'jasmine/headless/js_template'
autoload :JSTTemplateCache, 'jasmine/headless/jst_template_cache'
autoload :JSTTemplate, 'jasmine/headless/jst_template'
autoload :CSSTemplate, 'jasmine/headless/css_template'
autoload :NilTemplate, 'jasmine/headless/nil_template'
@ -35,24 +36,6 @@ module Jasmine
def root
@root ||= Pathname(File.expand_path('../../..', __FILE__))
end
def warn(message)
output.puts message if show_warnings?
end
def show_warnings=(show)
@show_warnings = show
end
def show_warnings?
@show_warnings = true if @show_warnings.nil?
@show_warnings
end
def output
$stdout
end
end
end
end

View File

@ -8,7 +8,21 @@ module Jasmine::Headless
alert_bad_format(file)
return ''
end
%{<script type="text/javascript">#{super}</script>}
begin
data = super
cache = Jasmine::Headless::JSTTemplateCache.new(file, data)
source = cache.handle
if cache.cached?
%{<script type="text/javascript" src="#{cache.cache_file}"></script>
<script type="text/javascript">window.CSTF['#{File.split(cache.cache_file).last}'] = '#{file}';</script>}
else
%{<script type="text/javascript">#{source}</script>}
end
rescue StandardError => e
puts "[%s] Error in compiling file: %s" % [ 'jst'.color(:red), file.color(:yellow) ]
raise e
end
end
end
end

View File

@ -0,0 +1,26 @@
require 'sprockets/jst_processor'
require 'digest/sha1'
require 'fileutils'
module Jasmine
module Headless
class JSTTemplateCache < CacheableAction
def initialize(file, data)
@file = file
@data = data
end
class << self
def cache_type
"jst_template"
end
end
def action
@data
end
end
end
end