2012-02-27 13:49:34 +00:00
|
|
|
require 'singleton'
|
|
|
|
|
|
|
|
module RablFastJson
|
|
|
|
class Library
|
|
|
|
include Singleton
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@cached_templates = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_rendered_template(source, context)
|
|
|
|
path = context.instance_variable_get(:@virtual_path)
|
2012-04-09 23:03:07 +00:00
|
|
|
@lookup_context = context.lookup_context
|
2012-03-02 10:32:11 +00:00
|
|
|
|
2012-03-27 16:35:36 +00:00
|
|
|
compiled_template = get_compiled_template(path, source)
|
2012-04-09 23:03:07 +00:00
|
|
|
|
|
|
|
format = context.params[:format] || 'json'
|
|
|
|
Renderers.const_get(format.upcase!).new(context).render(compiled_template)
|
2012-02-27 13:49:34 +00:00
|
|
|
end
|
|
|
|
|
2012-03-27 16:35:36 +00:00
|
|
|
def get_compiled_template(path, source)
|
2012-04-02 14:22:43 +00:00
|
|
|
if path && RablFastJson.cache_templates?
|
|
|
|
@cached_templates[path] ||= Compiler.new.compile_source(source)
|
2012-04-03 16:37:11 +00:00
|
|
|
@cached_templates[path].dup
|
2012-04-02 14:22:43 +00:00
|
|
|
else
|
|
|
|
Compiler.new.compile_source(source)
|
|
|
|
end
|
2012-02-27 13:49:34 +00:00
|
|
|
end
|
|
|
|
|
2012-03-27 16:35:36 +00:00
|
|
|
def get(path)
|
2012-02-27 13:49:34 +00:00
|
|
|
template = @cached_templates[path]
|
2012-04-15 16:17:49 +00:00
|
|
|
return template if template
|
2012-04-09 23:03:07 +00:00
|
|
|
t = @lookup_context.find_template(path, [], false)
|
2012-03-27 16:35:36 +00:00
|
|
|
get_compiled_template(path, t.source)
|
2012-02-27 13:49:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|