./lib/lux/template/template.rb in lux-fw-0.1.35 vs ./lib/lux/template/template.rb in lux-fw-0.2.1

- old
+ new

@@ -3,25 +3,30 @@ class Lux::Template @@template_cache = {} class << self - def render_with_layout template, opts={}, helper=nil - new(template, opts, helper).render_with_layout + def render_with_layout template, helper={} + new(template, helper).render_with_layout end - def render_part template, opts={}, helper=nil - new(template, opts, helper).render_part + def render_part template, helper={} + new(template, helper).render_part end end - def initialize template, opts={}, helper=nil - template = template.sub(/^[^\w]+/, '') + def initialize template, context={} + template = template.sub(/^[^\w]+/, '') @original_template = template - @opts = opts - @helper = helper + @helper = if context.class == Hash + # create helper class if only hash given + Lux::Helper.new(context) + else + context + end + mutex = Mutex.new if Lux.config(:auto_code_reload) && !Lux.thread[:template_cache] Lux.thread[:template_cache] = true mutex.synchronize { @@template_cache = {} } @@ -31,11 +36,11 @@ @tilt, @template = *ref return end for dir in ['./app/views'] - for ext in ['haml', 'erb'] + for ext in Tilt.default_mapping.template_map.keys next if @template test = "#{dir}/#{template}.#{ext}" @template = test if File.exists?(test) end end @@ -56,38 +61,35 @@ Lux.error("#{$!.message}\n\nTemplate: #{@template}") end end def render_part - base_class = @helper ? @helper.to_s : @template.split('/')[3] - base_class = nil if base_class.index('.') - base_class ||= Module.new - # global thread safe reference pointer to last temaplte rendered # we nned this for inline template render Lux.thread[:last_template_path] = @template.sub('/app/views','').sub(/\/[^\/]+$/,'').sub(/^\./,'') - Lux.page.files_in_use.push @template + Lux.current.files_in_use @template - helper = Lux::Helper.for(base_class, @opts) data = nil speed = Lux.speed do data = Lux::Error.try %[Lux::Template "#{@template}" render error] do - @tilt.render(helper) do + @tilt.render(@helper) do yield if block_given? end - end + end rescue Lux::Error.inline end Lux.log " #{@template.split('app/views/').last}, #{speed}" data end def render_with_layout @part_data = render_part + layout_path = "#{@original_template.split('/')[0]}/layout" - Lux::Template.new(layout_path, @opts).render_part do + + Lux::Template.new(layout_path, @helper).render_part do @part_data end end end