Class: Nanoc2::Layout

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc2/base/layout.rb

Overview

A Nanoc2::Layout represents a layout in a nanoc site. It has content, attributes (for determining which filter to use for laying out a page), a path (because layouts are organised hierarchically), and a modification time (to speed up compilation).

Constant Summary collapse

DEFAULTS =

Default values for layouts.

{
  :filter => 'erb'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, attributes, path, mtime = nil) ⇒ Layout

Creates a new layout.

content

The raw content of this layout.

attributes

A hash containing this layout’s attributes.

path

This layout’s path, starting and ending with a slash.

mtime

The time when this layout was last modified.



38
39
40
41
42
43
# File 'lib/nanoc2/base/layout.rb', line 38

def initialize(content, attributes, path, mtime=nil)
  @content    = content
  @attributes = attributes.clean
  @path       = path.cleaned_path
  @mtime      = mtime
end

Instance Attribute Details

#attributesObject (readonly)

A hash containing this layout’s attributes.



21
22
23
# File 'lib/nanoc2/base/layout.rb', line 21

def attributes
  @attributes
end

#contentObject (readonly)

The raw content of this layout.



18
19
20
# File 'lib/nanoc2/base/layout.rb', line 18

def content
  @content
end

#mtimeObject (readonly)

The time when this layout was last modified.



27
28
29
# File 'lib/nanoc2/base/layout.rb', line 27

def mtime
  @mtime
end

#pathObject (readonly)

This layout’s path, starting and ending with a slash.



24
25
26
# File 'lib/nanoc2/base/layout.rb', line 24

def path
  @path
end

#siteObject

The Nanoc2::Site this layout belongs to.



15
16
17
# File 'lib/nanoc2/base/layout.rb', line 15

def site
  @site
end

Instance Method Details

#attribute_named(name) ⇒ Object

Returns the attribute with the given name.



51
52
53
54
# File 'lib/nanoc2/base/layout.rb', line 51

def attribute_named(name)
  return @attributes[name] if @attributes.has_key?(name)
  return DEFAULTS[name]
end

#deleteObject

Deletes the layout. Tells the site’s data source to delete the layout.



79
80
81
82
83
# File 'lib/nanoc2/base/layout.rb', line 79

def delete
  @site.data_source.loading do
    @site.data_source.delete_layout(self)
  end
end

#filter_classObject

Returns the filter class needed for this layout.



57
58
59
# File 'lib/nanoc2/base/layout.rb', line 57

def filter_class
  Nanoc2::Filter.named(attribute_named(:filter))
end

#inspectObject



85
86
87
# File 'lib/nanoc2/base/layout.rb', line 85

def inspect
  "<#{self.class} path=#{self.path}>"
end

#move_to(new_path) ⇒ Object

Moves the layout to a new path. Tells the site’s data source to move the layout.



72
73
74
75
76
# File 'lib/nanoc2/base/layout.rb', line 72

def move_to(new_path)
  @site.data_source.loading do
    @site.data_source.move_layout(self, new_path)
  end
end

#saveObject

Saves the layout in the database, creating it if it doesn’t exist yet or updating it if it already exists. Tells the site’s data source to save the layout.



64
65
66
67
68
# File 'lib/nanoc2/base/layout.rb', line 64

def save
  @site.data_source.loading do
    @site.data_source.save_layout(self)
  end
end

#to_proxyObject

Returns a proxy (Nanoc2::LayoutProxy) for this layout.



46
47
48
# File 'lib/nanoc2/base/layout.rb', line 46

def to_proxy
  @proxy ||= LayoutProxy.new(self)
end