Class: Templette::Page

Inherits:
Object
  • Object
show all
Includes:
DataAccessors
Defined in:
lib/templette/page.rb

Overview

The Page is the core object of a Templette project. Pages won’t get generate unless there are available .yml files to build out the necessary info.

A general Page yaml structure example:

template_name: main
sections:
  title: Page Title
  nav:
    active-class: foo
    title: Foo!

The template_name will be used to load a template.

Everything in sections will be made available as methods in the template when it’s evaluated by by the rendering engine (ERB by default). Yaml hash items nested within others will be loaded into nested objects. To call the nav title, the template should call nav.title.

Defined Under Namespace

Classes: Section

Constant Summary collapse

PAGES_DEFAULT =
'pages/'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataAccessors

#attributes, #generate_accessors, #include_helpers, #method_missing, #partial

Methods included from Helpers

#image_tag, #script_tag, #stylesheet_tag

Constructor Details

#initialize(page_config) ⇒ Page

Returns a new instance of Page.

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/templette/page.rb', line 45

def initialize(page_config)
  raise PageError.new(self, "missing page #{page_config}") unless File.exists?(page_config)

  data = YAML::load_file(page_config)
  @name = page_config.dup
  @name.slice!(self.class.pages_dir)
  @name.chomp!('.yml')
  raise PageError.new(self, "missing required section \"template_name\" for page config #{page_config}") unless data['template_name']
  @template = Template.new(data['template_name'])

  raise PageError.new(self, "missing sections in yml for page config #{page_config}") unless data['sections']
  generate_accessors(data['sections'])
  include_helpers(template.helpers)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Templette::DataAccessors

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



25
26
27
# File 'lib/templette/page.rb', line 25

def name
  @name
end

#templateObject (readonly)

Returns the value of attribute template.



25
26
27
# File 'lib/templette/page.rb', line 25

def template
  @template
end

Class Method Details

.find_allObject

Grabs all of the yaml files found in /pages, and loads them as Page objects.



40
41
42
# File 'lib/templette/page.rb', line 40

def find_all
  Dir["#{pages_dir}**/*.yml"].map {|f| new f }
end

.pages_dirObject



30
31
32
# File 'lib/templette/page.rb', line 30

def pages_dir
  @@pages_dir ||= PAGES_DEFAULT
end

.pages_dir=(path) ⇒ Object



34
35
36
# File 'lib/templette/page.rb', line 34

def pages_dir=(path)
  @@pages_dir = path
end

Instance Method Details

#_bindingObject

:nodoc:



67
68
69
# File 'lib/templette/page.rb', line 67

def _binding # :nodoc:
  binding
end

#generate(out_dir) ⇒ Object



60
61
62
63
64
65
# File 'lib/templette/page.rb', line 60

def generate(out_dir)
  generate_subdirectory(out_dir)
  File.open(output_file_name(out_dir), 'w') do |f| 
    f << @template.render(binding)
  end
end

#pageObject

A requriement of the Templette::DataAccessors interface. Returns self.



72
# File 'lib/templette/page.rb', line 72

def page; self end