Module: Frank::Render
Constant Summary collapse
- TMPL_EXTS =
{ :html => %w[haml erb rhtml builder liquid textile md mkd markdown], :css => %w[sass less scss], :js => %w[coffee] }
- LAYOUT_EXTS =
%w[.haml .erb .rhtml .liquid]
Instance Method Summary collapse
-
#ext_from_handler(extension) ⇒ Object
lookup the original ext for given template path TODO: make non-ugly.
-
#layout_ext_or_first(layout_exts, ext) ⇒ Object
if the given ext is a layout ext, pop it off and return it otherwise return the first layout ext.
-
#layout_for(path) ⇒ Object
reverse walks the layouts folder until we find a layout returns nil if layout is not found.
-
#render(path, partial = false, local_vars = nil) ⇒ Object
render request path or template path.
-
#setup_page ⇒ Object
setup a new page object to be rendered.
-
#tilt(page, ext, source, locals = {}, &block) ⇒ Object
render a page using tilt and get the result template markup back.
-
#to_file_path(path) ⇒ Object
converts a request path to a template path.
Instance Method Details
#ext_from_handler(extension) ⇒ Object
lookup the original ext for given template path TODO: make non-ugly
143 144 145 146 147 148 149 |
# File 'lib/frank/base.rb', line 143 def ext_from_handler(extension) orig_ext = nil TMPL_EXTS.each do |ext, engines| orig_ext = ext.to_s if engines.include? extension[1..-1] end orig_ext end |
#layout_ext_or_first(layout_exts, ext) ⇒ Object
if the given ext is a layout ext, pop it off and return it otherwise return the first layout ext
182 183 184 |
# File 'lib/frank/base.rb', line 182 def layout_ext_or_first(layout_exts, ext) layout_exts.include?(ext) ? layout_exts.delete(ext) : layout_exts.first end |
#layout_for(path) ⇒ Object
reverse walks the layouts folder until we find a layout returns nil if layout is not found
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/frank/base.rb', line 154 def layout_for(path) layout_exts = LAYOUT_EXTS.dup ext = File.extname(path) default = 'default' << layout_ext_or_first(layout_exts, ext) file_path = path.sub(/\/[\w-]+\.[\w-]+$/, '') folders = file_path.split('/') until File.exist? File.join(Frank.root, Frank.layouts_folder, folders, default) break if layout_exts.empty? && folders.empty? if layout_exts.empty? layout_exts = LAYOUT_EXTS.dup default = 'default' << layout_ext_or_first(layout_exts, ext) folders.pop else default = 'default' << layout_exts.shift end end if File.exists? File.join(Frank.root, Frank.layouts_folder, folders, default) File.join(folders, default) else nil end end |
#render(path, partial = false, local_vars = nil) ⇒ Object
render request path or template path
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/frank/base.rb', line 74 def render(path, partial = false, local_vars = nil) @current_path = path unless partial # normalize the path path.sub!(/^\/?(.*)$/, '/\1') path.sub!(/\/$/, '/index.html') path.sub!(/(\/[\w-]+)$/, '\1.html') path = to_file_path(path) if defined? @request or path.match(/\/_[^\/]+$/) # regex for kinds that don't support meta # and define the meta delimiter , delimiter = /\/_|\.(scss|sass|less|coffee)$/, /^META-{3,}\s*$|^-{3,}META\s*$/ # set the layout layout = path.match() ? nil : layout_for(path) template_path = File.join(Frank.root, Frank.dynamic_folder, path) raise Frank::TemplateError, "Template not found #{template_path}" unless File.exist? template_path # read in the template # check for meta and parse it if it exists template = File.read(template_path) << "\n" ext = File.extname(path) template, = template.split(delimiter).reverse locals = (, local_vars) # use given layout if defined as a meta field layout = locals[:layout] == 'nil' ? nil : locals[:layout] if locals.has_key?(:layout) page = setup_page # let tilt determine the template handler # and return some template markup if layout.nil? tilt(page, ext, template, locals) else layout_path = File.join(Frank.root, Frank.layouts_folder, layout) # add layout_path to locals raise Frank::TemplateError, "Layout not found #{layout_path}" unless File.exist? layout_path page_content = tilt(page, ext, template, locals) tilt(page, File.extname(layout), layout_path, locals) do page_content end end end |
#setup_page ⇒ Object
setup a new page object to be rendered
199 200 201 202 203 204 205 206 207 |
# File 'lib/frank/base.rb', line 199 def setup_page page = Object.new.extend(TemplateHelpers).extend(Render) instance_variables.each do |var| unless ['@response', '@env'].include? var page.instance_variable_set(var.intern, instance_variable_get(var)) end end page end |
#tilt(page, ext, source, locals = {}, &block) ⇒ Object
render a page using tilt and get the result template markup back
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/frank/base.rb', line 187 def tilt(page, ext, source, locals={}, &block) Tilt[ext].new do source = source.to_str if source.respond_to?(:to_str) if source.match(/^[^\n]+$/) && File.exist?(source) File.read(source) else source end end.render(page, locals=locals, &block) end |
#to_file_path(path) ⇒ Object
converts a request path to a template path
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/frank/base.rb', line 122 def to_file_path(path) file_name = File.basename(path, File.extname(path)) file_ext = File.extname(path).sub(/^\./, '') folder = File.join(Frank.root, Frank.dynamic_folder) engine = nil TMPL_EXTS.each do |ext, engines| if ext.to_s == file_ext engine = engines.reject do |eng| !File.exist? File.join(folder, path.sub(/\.[\w-]+$/, ".#{eng}")) end.first end end raise Frank::TemplateError, "Template not found #{path}" if engine.nil? path.sub(/\.[\w-]+$/, ".#{engine}") end |