2014-01-03 22:15:02 +01:00
require 'tilt'
require 'active_support/core_ext/string/output_safety'
require 'middleman-core/template_context'
require 'middleman-core/file_renderer'
module Middleman
class TemplateRenderer
2014-07-05 20:17:41 +02:00
extend Forwardable
2014-01-03 22:15:02 +01:00
def self . cache
@_cache || = :: Tilt :: Cache . new
end
2014-07-05 20:17:41 +02:00
def_delegator :" self.class " , :cache
2014-01-03 22:15:02 +01:00
# Custom error class for handling
class TemplateNotFound < RuntimeError ; end
def initialize ( app , path )
@app = app
@path = path
end
# Render a template, with layout, given a path
#
# @param [Hash] locs
# @param [Hash] opts
# @return [String]
2014-01-04 00:49:54 +01:00
def render ( locs = { } , opts = { } )
2014-01-03 22:15:02 +01:00
path = @path . dup
extension = File . extname ( path )
engine = extension [ 1 .. - 1 ] . to_sym
if defined? ( :: I18n )
old_locale = :: I18n . locale
:: I18n . locale = opts [ :lang ] if opts [ :lang ]
end
# Sandboxed class for template eval
context = @app . template_context_class . new ( @app , locs , opts )
2014-04-07 21:43:16 +02:00
# TODO: Only for HAML files
2014-04-29 20:43:05 +02:00
context . init_haml_helpers if context . respond_to? ( :init_haml_helpers )
2014-01-03 22:15:02 +01:00
# Keep rendering template until we've used up all extensions. This
# handles cases like `style.css.sass.erb`
content = nil
while :: Tilt [ path ]
begin
opts [ :template_body ] = content if content
content_renderer = :: Middleman :: FileRenderer . new ( @app , path )
content = content_renderer . render ( locs , opts , context )
path = File . basename ( path , File . extname ( path ) )
rescue LocalJumpError
2014-02-23 03:24:40 +01:00
raise " Tried to render a layout (calls yield) at #{ path } like it was a template. Non-default layouts need to be in #{ source } / #{ @app . config [ :layouts_dir ] } . "
2014-01-03 22:15:02 +01:00
end
end
# If we need a layout and have a layout, use it
if layout_path = fetch_layout ( engine , opts )
layout_renderer = :: Middleman :: FileRenderer . new ( @app , layout_path )
content = layout_renderer . render ( locs , opts , context ) { content }
end
# Return result
content
ensure
# Pop all the saved variables from earlier as we may be returning to a
# previous render (layouts, partials, nested layouts).
:: I18n . locale = old_locale if defined? ( :: I18n )
end
2014-04-29 19:50:21 +02:00
protected
2014-01-03 22:15:02 +01:00
# Find a layout for a given engine
#
# @param [Symbol] engine
# @param [Hash] opts
# @return [String]
def fetch_layout ( engine , opts )
# The layout name comes from either the system default or the options
2014-04-29 19:50:21 +02:00
local_layout = opts . key? ( :layout ) ? opts [ :layout ] : @app . config [ :layout ]
2014-01-03 22:15:02 +01:00
return false unless local_layout
# Look for engine-specific options
engine_options = @app . config . respond_to? ( engine ) ? @app . config . send ( engine ) : { }
# The engine for the layout can be set in options, engine_options or passed
# into this method
2014-04-29 19:50:21 +02:00
layout_engine = if opts . key? ( :layout_engine )
2014-01-03 22:15:02 +01:00
opts [ :layout_engine ]
2014-04-29 19:50:21 +02:00
elsif engine_options . key? ( :layout_engine )
2014-01-03 22:15:02 +01:00
engine_options [ :layout_engine ]
else
engine
end
# Automatic mode
if local_layout == :_auto_layout
# Look for :layout of any extension
# If found, use it. If not, continue
locate_layout ( :layout , layout_engine ) || false
else
# Look for specific layout
# If found, use it. If not, error.
if layout_path = locate_layout ( local_layout , layout_engine )
layout_path
else
raise :: Middleman :: TemplateRenderer :: TemplateNotFound , " Could not locate layout: #{ local_layout } "
end
end
end
# Find a layout on-disk, optionally using a specific engine
# @param [String] name
# @param [Symbol] preferred_engine
# @return [String]
def locate_layout ( name , preferred_engine = nil )
self . class . locate_layout ( @app , name , preferred_engine )
end
# Find a layout on-disk, optionally using a specific engine
# @param [String] name
# @param [Symbol] preferred_engine
# @return [String]
def self . locate_layout ( app , name , preferred_engine = nil )
2014-02-23 03:24:40 +01:00
resolve_opts = { }
2014-04-29 19:50:21 +02:00
resolve_opts [ :preferred_engine ] = preferred_engine unless preferred_engine . nil?
2014-01-03 22:15:02 +01:00
2014-02-23 03:24:40 +01:00
# Check layouts folder
layout_path = resolve_template ( app , File . join ( app . config [ :layouts_dir ] , name . to_s ) , resolve_opts )
2014-01-03 22:15:02 +01:00
2014-02-23 03:24:40 +01:00
# If we didn't find it, check root
layout_path = resolve_template ( app , name , resolve_opts ) unless layout_path
2014-01-03 22:15:02 +01:00
# Return the path
layout_path
end
# Find a template on disk given a output path
# @param [String] request_path
# @param [Hash] options
# @return [Array<String, Symbol>, Boolean]
def resolve_template ( request_path , options = { } )
self . class . resolve_template ( @app , request_path , options )
end
# Find a template on disk given a output path
# @param [String] request_path
2014-02-23 03:24:40 +01:00
# @option options [Boolean] :preferred_engine If set, try this engine first, then fall back to any engine.
# @return [String, Boolean] Either the path to the template, or false
2014-01-03 22:15:02 +01:00
def self . resolve_template ( app , request_path , options = { } )
# Find the path by searching or using the cache
request_path = request_path . to_s
cache . fetch ( :resolve_template , request_path , options ) do
relative_path = Util . strip_leading_slash ( request_path )
on_disk_path = File . expand_path ( relative_path , app . source_dir )
# By default, any engine will do
2014-02-23 03:24:40 +01:00
preferred_engines = [ '*' ]
2014-01-03 22:15:02 +01:00
2014-02-23 03:24:40 +01:00
# If we're specifically looking for a preferred engine
2014-04-29 19:50:21 +02:00
if options . key? ( :preferred_engine )
2014-01-03 22:15:02 +01:00
extension_class = :: Tilt [ options [ :preferred_engine ] ]
# Get a list of extensions for a preferred engine
2014-04-29 20:43:05 +02:00
matched_exts = :: Tilt . mappings . select do | _ , engines |
2014-02-23 03:24:40 +01:00
engines . include? extension_class
end . keys
2014-01-03 22:15:02 +01:00
2014-02-23 03:24:40 +01:00
# Prefer to look for the matched extensions
unless matched_exts . empty?
preferred_engines . unshift ( '{' + matched_exts . join ( ',' ) + '}' )
2014-01-03 22:15:02 +01:00
end
end
2014-07-08 08:43:09 +02:00
search_paths = preferred_engines . map do | preferred_engine |
on_disk_path + '.' + preferred_engine
2014-01-03 22:15:02 +01:00
end
2014-02-23 03:24:40 +01:00
found_path = nil
search_paths . each do | path_with_ext |
found_path = Dir [ path_with_ext ] . find do | path |
2014-01-03 22:15:02 +01:00
:: Tilt [ path ]
end
2014-02-23 03:24:40 +01:00
break if found_path
2014-01-03 22:15:02 +01:00
end
2014-02-23 03:24:40 +01:00
# If we found one, return it
if found_path
found_path
2014-04-29 19:50:21 +02:00
elsif File . exist? ( on_disk_path )
2014-02-23 03:24:40 +01:00
on_disk_path
2014-01-03 22:15:02 +01:00
else
false
end
end
end
end
end