1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00

Change the behavior of before_render and after_render hooks from #761.

This changes the before_render and after_render hooks so that the callbacks
given to them each get to modify the template source / the rendered output
in turn, instead of having to modify them in-place. It also changes the arguments
provided to the callbacks - now, they are given the source/output, the template path,
the locals hash, and the Tilt template *class*, wheras previously they were given the source/output
and the Tilt template *instance*. The locals hash is there in case pron case hooks want to
pay attention to the locals in some way. The before_render hook is also run before
compiling the template and caching it, which should prevent weird data mismatches
and save some template compilation effort.
This commit is contained in:
Ben Hollis 2013-02-10 12:05:07 -08:00
parent 5e12c68f1b
commit 3bdbc11db2

View file

@ -246,15 +246,26 @@ module Middleman
options = opts.merge(options_for_ext(extension))
options[:outvar] ||= '@_out_buf'
template_class = Tilt[path]
# Allow hooks to manipulate the template before render
self.class.callbacks_for_hook(:before_render).each do |callback|
newbody = callback.call(body, path, locs, template_class)
body = newbody if newbody # Allow the callback to return nil to skip it
end
# Read compiled template from disk or cache
template = cache.fetch(:compiled_template, options, body) do
::Tilt.new(path, 1, options) { body }
end
# Render using Tilt
run_hook :before_render, template.data, template
content = template.render(context, locs, &block)
run_hook :after_render, content, template
content = template.render(context, path, locs, &block)
# Allow hooks to manipulate the result after render
self.class.callbacks_for_hook(:after_render).each do |callback|
content = callback.call(content, locs, template_class)
end
return content
ensure
# Reset stored buffer