Module: Frank::Render
Constant Summary collapse
- TMPL_EXTS =
{ :html => %w[haml erb rhtml builder liquid mustache], :css => %w[sass less], :js => %w[coffee] }
Instance Method Summary collapse
- #find_template_ext(filename) ⇒ Object
- #get_layout_for(view) ⇒ Object
- #name_ext(path) ⇒ Object
- #remove_ext(path) ⇒ Object
- #render_path(path) ⇒ Object
- #render_template(tmpl, *args) ⇒ Object
- #render_with_layout(tmpl, *args) ⇒ Object
- #reverse_ext_lookup(ext) ⇒ Object
- #tilt_lang(file, lang, *tilt_args, &block) ⇒ Object
- #tilt_with_request(file, *args, &block) ⇒ Object
Instance Method Details
#find_template_ext(filename) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/frank/base.rb', line 111 def find_template_ext(filename) name, kind = name_ext(filename) kind = reverse_ext_lookup(kind) if kind && TMPL_EXTS[kind.intern].nil? TMPL_EXTS[ kind.nil? ? :html : kind.intern ].each do |ext| tmpl = "#{(name||'')}.#{ext}" return [tmpl, kind] if File.exists? File.join(@dynamic_folder, tmpl) end TMPL_EXTS[ kind.nil? ? :html : kind.intern ].each do |ext| default = File.join((name||''), "#{@templates['default']}.#{ext}") return [default, kind] if File.exists? File.join(@dynamic_folder, default) end nil rescue nil end |
#get_layout_for(view) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/frank/base.rb', line 129 def get_layout_for(view) view, ext = name_ext(view) layouts = @templates['layouts'] || [] onlies = layouts.select {|l| l['only'] } nots = layouts.select {|l| l['not'] } blanks = layouts - onlies - nots layout = onlies.select {|l| l['only'].index(view) }.first layout = nots.reject {|l| l['not'].index(view) }.first unless layout layout = blanks.first unless layout layout.nil? ? nil : layout['name'] + '.' + ext end |
#name_ext(path) ⇒ Object
70 71 72 |
# File 'lib/frank/base.rb', line 70 def name_ext(path) return path.split(/\.(?=[^.]+$)/) end |
#remove_ext(path) ⇒ Object
156 157 158 |
# File 'lib/frank/base.rb', line 156 def remove_ext(path) path.gsub(File.extname(path), '') end |
#render_path(path) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/frank/base.rb', line 74 def render_path(path) path.sub!(/^\//,'') template, ext = find_template_ext(path) raise Errno::ENOENT if template.nil? if template.match(/^_/) or (ext||'').match(/^(js|css)$/) render_template template else render_with_layout template end end |
#render_template(tmpl, *args) ⇒ Object
86 87 88 |
# File 'lib/frank/base.rb', line 86 def render_template(tmpl, *args) tilt_with_request(File.join(@dynamic_folder, tmpl), *args) {"CONTENT"} end |
#render_with_layout(tmpl, *args) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/frank/base.rb', line 90 def render_with_layout(tmpl, *args) if layout = get_layout_for(tmpl) tilt_with_request(File.join(@dynamic_folder, layout), *args) do render_template tmpl end else render_template tmpl end end |
#reverse_ext_lookup(ext) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/frank/base.rb', line 104 def reverse_ext_lookup(ext) TMPL_EXTS.each do |kind, exts| return kind.to_s if exts.index(ext) end nil end |
#tilt_lang(file, lang, *tilt_args, &block) ⇒ Object
144 145 146 |
# File 'lib/frank/base.rb', line 144 def tilt_lang(file, lang, *tilt_args, &block) Tilt[lang].new(file, 1).render(*tilt_args, &block) end |
#tilt_with_request(file, *args, &block) ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/frank/base.rb', line 148 def tilt_with_request(file, *args, &block) locals = @request.nil? ? {} : { :request => @env, :params => @request.params } obj = Object.new.extend(TemplateHelpers).extend(Render) obj.instance_variable_set(:@dynamic_folder, @dynamic_folder) obj.instance_variable_set(:@templates, @templates) Tilt.new(file, 1).render(obj, locals, &block) end |