middleman/middleman-core/lib/middleman-core/template_context.rb

171 lines
5.5 KiB
Ruby
Raw Normal View History

require 'pathname'
require 'middleman-core/file_renderer'
require 'middleman-core/template_renderer'
2014-01-02 06:19:05 +01:00
module Middleman
# The TemplateContext Class
#
# A clean context, separate from Application, in which templates can be executed.
# All helper methods and values available in a template, but be accessible here.
# Also implements two helpers: wrap_layout & render (used by padrino's partial method).
# A new context is created for each render of a path, but that context is shared through
# the request, passed from template, to layouts and partials.
2014-01-02 06:19:05 +01:00
class TemplateContext
# Allow templates to directly access the current app instance.
# @return [Middleman::Application]
2014-01-02 06:19:05 +01:00
attr_reader :app
# Required for Padrino's rendering
attr_accessor :current_engine
2014-01-02 06:19:05 +01:00
# Shorthand references to global values on the app instance.
2014-06-07 00:32:00 +02:00
delegate :config, :logger, :sitemap, :server?, :build?, :environment?, :data, :extensions, :source_dir, :root, to: :app
2014-01-02 06:19:05 +01:00
# Initialize a context with the current app and predefined locals and options hashes.
#
# @param [Middleman::Application] app
# @param [Hash] locs
# @param [Hash] opts
2014-01-02 06:19:05 +01:00
def initialize(app, locs={}, opts={})
@app = app
@locs = locs.dup.freeze
@opts = opts.dup.freeze
2014-01-02 06:19:05 +01:00
end
# Return the current buffer to the caller and clear the value internally.
# Used when moving between templates when rendering layouts or partials.
#
# @api private
# @return [String] The old buffer.
2014-01-02 06:19:05 +01:00
def save_buffer
@_out_buf, buf_was = '', @_out_buf
buf_was
2014-01-02 06:19:05 +01:00
end
# Restore a previously saved buffer.
#
# @api private
# @param [String] buf_was
# @return [void]
def restore_buffer(buf_was)
@_out_buf = buf_was
2014-01-02 06:19:05 +01:00
end
# Allow layouts to be wrapped in the contents of other layouts.
#
2014-01-02 06:19:05 +01:00
# @param [String, Symbol] layout_name
# @return [void]
def wrap_layout(layout_name, &block)
# Save current buffer for later
buf_was = save_buffer
2014-01-02 06:19:05 +01:00
# Find a layout for this file
2014-04-29 19:50:21 +02:00
layout_path = ::Middleman::TemplateRenderer.locate_layout(@app, layout_name, current_engine)
2014-01-02 06:19:05 +01:00
# Get the layout engine
2014-01-02 06:19:05 +01:00
extension = File.extname(layout_path)
engine = extension[1..-1].to_sym
# Store last engine for later (could be inside nested renders)
2014-04-29 19:50:21 +02:00
self.current_engine, engine_was = engine, current_engine
2014-01-02 06:19:05 +01:00
# By default, no content is captured
content = ''
# Attempt to capture HTML from block
2014-01-02 06:19:05 +01:00
begin
content = capture_html(&block) if block_given?
2014-01-02 06:19:05 +01:00
ensure
# Reset stored buffer, regardless of success
restore_buffer(buf_was)
2014-01-02 06:19:05 +01:00
end
# Render the layout, with the contents of the block inside.
concat_safe_content render_file(layout_path, @locs, @opts) { content }
2014-01-02 06:19:05 +01:00
ensure
# Reset engine back to template's value, regardless of success
2014-01-02 06:19:05 +01:00
self.current_engine = engine_was
end
# Sinatra/Padrino compatible render method signature referenced by some view
# helpers. Especially partials.
#
# @param [] _ Unused parameter.
# @param [String, Symbol] name The partial to render.
2014-01-02 06:19:05 +01:00
# @param [Hash] options
# @return [String]
def render(_, name, options={}, &block)
name = name.to_s
2014-01-02 06:19:05 +01:00
partial_path = locate_partial_relative(name) || locate_partial_in_partials_dir(name)
2014-01-02 06:19:05 +01:00
raise ::Middleman::TemplateRenderer::TemplateNotFound, "Could not locate partial: #{name}" unless partial_path
2014-01-02 06:19:05 +01:00
opts = options.dup
locs = opts.delete(:locals)
2014-01-02 06:19:05 +01:00
render_file(partial_path, locs.freeze, opts.freeze, &block)
end
2014-01-02 06:19:05 +01:00
protected
2014-01-02 06:19:05 +01:00
# Locate a partial reltive to the current path, given a name.
#
# @api private
# @param [String] name
# @return [String]
def locate_partial_relative(name)
return unless resource = sitemap.find_resource_by_path(current_path)
# Look for partials relative to the current path
current_dir = File.dirname(resource.source_file)
relative_dir = File.join(current_dir.sub(%r{^#{Regexp.escape(source_dir)}/?}, ''), name)
::Middleman::TemplateRenderer.resolve_template(
@app,
relative_dir,
try_without_underscore: true,
preferred_engine: File.extname(resource.source_file)[1..-1].to_sym
)
end
2014-01-02 06:19:05 +01:00
# Locate a partial reltive to the partials dir, given a name.
#
# @api private
# @param [String] name
# @return [String]
def locate_partial_in_partials_dir(name)
partials_path = File.join(@app.config[:partials_dir], name)
::Middleman::TemplateRenderer.resolve_template(
@app,
partials_path,
try_without_underscore: true
)
end
# Render a path with locs, opts and contents block.
#
# @api private
# @param [String] path The file path.
# @param [Hash] locs Template locals.
# @param [Hash] opts Template options.
# @param [Proc] block A block will be evaluated to return internal contents.
# @return [String] The resulting content string.
def render_file(path, locs, opts, &block)
file_renderer = ::Middleman::FileRenderer.new(@app, path)
file_renderer.render(locs, opts, self, &block)
2014-01-02 06:19:05 +01:00
end
def current_path
@locs[:current_path]
end
# Get the resource object for the current path
# @return [Middleman::Sitemap::Resource]
def current_resource
return nil unless current_path
sitemap.find_resource_by_destination_path(current_path)
end
alias_method :current_page, :current_resource
2014-01-02 06:19:05 +01:00
end
end