Module: Tadpole::Template

Defined in:
lib/tadpole/template.rb

Defined Under Namespace

Modules: ClassMethods Classes: MissingSectionError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/tadpole/template.rb', line 59

def method_missing(meth, *args, &block)
  if options.respond_to?(meth)
    options[meth]
  elsif meth.to_s =~ /=$/ && options.has_key?(meth.to_s.gsub(/=$/, ''))
    options[meth] = *args
  else
    super
  end
end

Instance Attribute Details

#current_sectionObject

Returns the value of attribute current_section.



47
48
49
# File 'lib/tadpole/template.rb', line 47

def current_section
  @current_section
end

#optionsObject

Returns the value of attribute options.



46
47
48
# File 'lib/tadpole/template.rb', line 46

def options
  @options
end

#subsectionsObject

Returns the value of attribute subsections.



47
48
49
# File 'lib/tadpole/template.rb', line 47

def subsections
  @subsections
end

Class Method Details

.included(klass) ⇒ Object



42
43
44
# File 'lib/tadpole/template.rb', line 42

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#all_sections(&block) ⇒ Object



180
181
182
183
184
185
# File 'lib/tadpole/template.rb', line 180

def all_sections(&block)
  subsections.each do |s|
    next if Array === s
    yield section_name(s)
  end
end

#compile_sections(list = sections) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/tadpole/template.rb', line 83

def compile_sections(list = sections)
  return unless Array === list
  list.map do |section|
    case section
    when Array
      compile_sections(section)
    when String, Symbol
      if respond_to?(section)
        section
      else
        find_section_provider(section)
      end
    when Module
      section.new(options)
    else
      section
    end
  end
end

#initObject



115
# File 'lib/tadpole/template.rb', line 115

def init; end

#initialize(opts = {}, &block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tadpole/template.rb', line 103

def initialize(opts = {}, &block)
  self.options = opts
  @providers = {}
  #self.sections(*sections)
  
  init(&block)

  if Tadpole.caching
    @compiled_sections = compile_sections(sections) 
  end
end

#inspectObject



210
211
212
213
# File 'lib/tadpole/template.rb', line 210

def inspect
  "#<Template:0x%s path='%s' sections=%s%s>" % [object_id.to_s(16), 
    path, sections.inspect, @compiled_sections ? ' (compiled)' : ''] 
end

#pathObject



70
# File 'lib/tadpole/template.rb', line 70

def path; self.class.path end

#render(section, locals = {}, call_method = false, &block) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/tadpole/template.rb', line 191

def render(section, locals = {}, call_method = false, &block)
  case section
  when String, Symbol
    if call_method && respond_to?(section) 
      send(section, &block)
    else
      find_section_provider(section).render(locals, &block)
    end
  when Template
    section.run(locals, &block)
  when Module
    section.new(options).run(locals, &block)
  when SectionProviders::SectionProvider
    section.render(locals, &block)
  else
    raise MissingSectionError, "missing section #{section.inspect}"
  end
end

#run(opts = {}, &block) ⇒ Object Also known as: to_s



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/tadpole/template.rb', line 117

def run(opts = {}, &block)
  return '' if run_before_run.is_a?(FalseClass)
  
  old_opts = options
  self.options = options.to_hash.update(opts)
  out = run_sections(@compiled_sections || sections, &block)
  self.options = old_opts
  out
rescue => e
  provider = find_section_provider(current_section)
  line = provider.full_path
  line += " (template)" if provider.is_a?(SectionProviders::TemplateProvider)
  line += ":1:in#{@compiled_sections ? ' compiled' : ''} section `#{current_section}'"
  me = NoMethodError.new(e.message)
  me.set_backtrace([line] + e.backtrace)
  raise me
end

#run_sections(sects, break_first = false, locals = {}, &block) ⇒ Object

Raises:

  • (ArgumentError)


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/tadpole/template.rb', line 136

def run_sections(sects, break_first = false, locals = {}, &block)
  out = ''
  raise ArgumentError, "Template(#{path}) is missing sections" unless sects
  sects = sects.first if sects.first.is_a?(Array)
  sects.each_with_index do |section, i|
    (break_first ? break : next) if section.is_a?(Array)
    
    self.current_section = section_name(section)
    
    next if run_before_sections.is_a?(FalseClass)

    if sects[i+1].is_a?(Array)
      old, self.subsections = subsections, sects[i+1]
      out += run_subsections(section, sects[i+1], locals, &block)
      self.subsections = old
    else
      out += render(section, locals, true, &block)
    end
    
    break if break_first
  end
  out
end

#run_subsections(section, subsections, locals = {}, &block) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/tadpole/template.rb', line 160

def run_subsections(section, subsections, locals = {}, &block)
  list = subsections.dup

  render(section, locals, true) do |*args|
    if list.empty?
      raise LocalJumpError, "Section `#{section}' yielded with no sub-section given."
    end
    
    ysection, locals = *parse_yield_args(*args)
    if ysection
      render(ysection, locals, true)
    else
      data = run_sections(list, true, locals, &block) 
      list.shift; list.shift if list.first.is_a?(Array)
      list = subsections.dup if list.empty? 
      data
    end
  end
end

#sections(*new_sections) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/tadpole/template.rb', line 72

def sections(*new_sections)
  new_sections.compact!
  if new_sections.empty? 
    @sections 
  elsif new_sections.size == 1 && new_sections.first.is_a?(Array)
    @sections = new_sections.first
  else
    @sections = new_sections
  end
end

#template_pathsObject



69
# File 'lib/tadpole/template.rb', line 69

def template_paths; self.class.template_paths end

#yieldall(locals = {}, &block) ⇒ Object



187
188
189
# File 'lib/tadpole/template.rb', line 187

def yieldall(locals = {}, &block)
  run_sections(subsections, false, locals, &block) 
end