Sha256: 72519e16b57df1b20512610fb4a9fe8bfa0ff7cea7a4184b895e729fd1b2466e
Contents?: true
Size: 1.67 KB
Versions: 18
Compression:
Stored size: 1.67 KB
Contents
module Plate # Callbacks are used to extend the base functionality of Plate. You might want to create # some dynamic pages after all posts have been loaded (such as a blog archives view) or # clean up some page output after it is rendered but before it is written to disk. Callbacks # are the best way to achieve these and other common use cases. # # Callbacks must be registered with an object to be used. If you generated a new site # using the `plate new .` or `platify` command, you should see an example callbacks file # in `lib/callbacks.rb`. # module Callbacks def self.included(base) base.send(:include, InstanceMethods) base.send(:extend, ClassMethods) end module ClassMethods # All of the callbacks that have been registered. def callbacks @callbacks ||= {} end # Register a new callback # # @example # Plate::Page.register_callback(:after_render) do |page| # puts "Rendered page! #{page.path}" # end def register_callback(name, method_name = nil, &block) callbacks[name] ||= [] callbacks[name] << (block || method_name) end end module InstanceMethods def around_callback(name, &block) run_callback "before_#{name}".to_sym block.call run_callback "after_#{name}".to_sym end def run_callback(name) if callbacks = self.class.callbacks[name] callbacks.each do |callback| if Proc === callback callback.call(self) elsif self.respond_to?(callback) self.send(callback) end end end end end end end
Version data entries
18 entries across 18 versions & 1 rubygems