Class: Plate::DynamicPage

Inherits:
Page
  • Object
show all
Defined in:
lib/plate/dynamic_page.rb

Overview

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.

Examples:

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

Instance Attribute Summary collapse

Attributes inherited from Page

#body, #content, #file, #layout, #meta, #partials, #raw, #site

Instance Method Summary collapse

Methods inherited from Page

#<=>, #==, #base_path, #basename, #directory, #downgrade?, #engines, #extension, #extensions, #file?, #file_name, #format_extension, #id, #inspect, #keywords, #load!, #loaded?, #path, #relative_file, #reload!, #rendered_content, #slug, #title_for_url, #to_s, #url, #write!

Methods included from Callbacks

included

Constructor Details

#initialize(site, destination_path, meta = {}) ⇒ DynamicPage

Set up a new instance of Dynamic Page



30
31
32
33
34
35
36
37
# File 'lib/plate/dynamic_page.rb', line 30

def initialize(site, destination_path, meta = {})
  self.site = site
  self.path = destination_path
  self.meta = meta
  self.content = ""
  self.locals = {}
  self.partials = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

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` `locals` or just `local_key`. They all will return the same value (provided :local_key exists in the locals of course)



75
76
77
78
# File 'lib/plate/dynamic_page.rb', line 75

def method_missing(sym, *args)
  return locals[sym] if locals.has_key?(sym)
  super
end

Instance Attribute Details

#file_pathObject

The full file system path of where this file will be written to. Set from the destination path on initialize.



23
24
25
# File 'lib/plate/dynamic_page.rb', line 23

def file_path
  @file_path
end

#localsObject

Provides a hash to the page with variables that are accessible from with dynamic layouts or dynamic templates for this page.



27
28
29
# File 'lib/plate/dynamic_page.rb', line 27

def locals
  @locals
end

Instance Method Details

#nameObject

name for dynamic pages is the path



40
41
42
# File 'lib/plate/dynamic_page.rb', line 40

def name
  self.file_path
end

#path=(destination_path) ⇒ Object

Set the destination path for this page.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/plate/dynamic_page.rb', line 51

def path=(destination_path)
  # Remove leading and trailing slashes
  destination_path = destination_path.to_s.gsub(/^\//, '').gsub(/\/$/, '')

  # Unless file ends in an extension, add /index.html
  unless destination_path =~ /(.*?)\.[a-z0-0]{2,4}/i
    destination_path = "#{destination_path}/index.html"
  end

  destination_path = "/#{destination_path}"

  self.file_path = destination_path
end

#rendered_bodyObject

Alias for the content that is provided



66
67
68
# File 'lib/plate/dynamic_page.rb', line 66

def rendered_body
  self.content
end