Class: Lux::Template

Inherits:
Object show all
Defined in:
lib/lux/template/template.rb

Constant Summary collapse

@@template_cache =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, context = {}) ⇒ Template

Returns a new instance of Template.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lux/template/template.rb', line 17

def initialize template, context={}
  template           = template.sub(/^[^\w]+/, '')
  @original_template = template

  @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 = {} }
  end

  if ref = @@template_cache[template]
    @tilt, @template = *ref
    return
  end

  for dir in ['./app/views']
    for ext in Tilt.default_mapping.template_map.keys
      next if @template
      test = "#{dir}/#{template}.#{ext}"
      @template = test if File.exists?(test)
    end
  end

  unless @template
    err = caller.reject{ |l| l =~ %r{(/lux/|/gems/)} }.map{ |l| el=l.to_s.split(Lux.root.to_s); el[1] || l }.join("\n")
    Lux.error %[Lux::Template "#{template}.{erb,haml}" not found\n\n#{err}]
    raise
  end

  begin
    mutex.synchronize do
      # @tilt = Tilt.new(@template, ugly:true, escape_html: false)
      @tilt = Tilt.new(@template, escape_html: false)
      @@template_cache[template] = [@tilt, @template]
    end
  rescue
    Lux.error("#{$!.message}\n\nTemplate: #{@template}")
  end
end

Class Method Details

.render_part(template, helper = {}) ⇒ Object



12
13
14
# File 'lib/lux/template/template.rb', line 12

def render_part template, helper={}
  new(template, helper).render_part
end

.render_with_layout(template, helper = {}) ⇒ Object



8
9
10
# File 'lib/lux/template/template.rb', line 8

def render_with_layout template, helper={}
  new(template, helper).render_with_layout
end

Instance Method Details

#render_partObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/lux/template/template.rb', line 65

def render_part
  # 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.current.files_in_use @template

  data = nil
  speed = Lux.speed do
    data = Lux::Error.try %[Lux::Template "#{@template}" render error] do
      @tilt.render(@helper) do
        yield if block_given?
      end
    end rescue Lux::Error.inline
  end

  Lux.log " #{@template.split('app/views/').last}, #{speed}"

  data
end

#render_with_layoutObject



86
87
88
89
90
91
92
93
94
# File 'lib/lux/template/template.rb', line 86

def render_with_layout
  @part_data = render_part

  layout_path = "#{@original_template.split('/')[0]}/layout"

  Lux::Template.new(layout_path, @helper).render_part do
    @part_data
  end
end