module Plate # Allows the creation of one-off pages generated entirely from a dynamic source. The root # content for a `DynamicPage` instance is not read from the source directory, as opposed # to normal files. Instead, dynamic pages are typically used to create pages with dynamic # data and content. # # Examples may include a blog archives page, list of tags, categories pages, etc. # # @example Generate a page with custom content # # # This callback creates a tag index page for each tag in your posts. # callback :site, :after_render do |site| # site.tags.each do |tag| # tag_page = DynamicPage.new(site, "/tags/#{site.to_url(tag)}/index.html", :layout => 'tag') # tag_page.locals = { :tag => tag } # tag_page.write! # end # end # class DynamicPage < Page # The full file system path of where this file will be written to. Set from the # destination path on initialize. attr_accessor :file_path # Provides a hash to the page with variables that are accessible from with dynamic # layouts or dynamic templates for this page. attr_accessor :locals # Set up a new instance of Dynamic Page def initialize(site, destination_path, meta = {}) self.site = site self.file_path = destination_path self.meta = meta self.content = "" self.locals = {} end # Set the full hash to be provided to the view. Hash keys are symbolized before # sending to the view. def locals=(hash) @locals = hash.symbolize_keys! end # Alias for the content that is provided def rendered_body self.content end # Check to see if a method called is in your locals. Allows the view to call variables in the locals # hash directly without having to call `page.locals`. # # For example, in a view you can call `page.locals[:local_key]` `locals[:local_key]` or just `local_key`. # They all will return the same value (provided :local_key exists in the locals of course) def method_missing(sym, *args) return locals[sym] if locals.has_key?(sym) super end end end