Class: Plate::Layout

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, file = nil, load_on_initialize = true) ⇒ Layout

Returns a new instance of Layout.



5
6
7
8
9
10
11
12
# File 'lib/plate/layout.rb', line 5

def initialize(site, file = nil, load_on_initialize = true)
  self.site = site
  self.file = file
  self.meta = {}
  self.content = ""

  load! if load_on_initialize and file?
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



3
4
5
# File 'lib/plate/layout.rb', line 3

def content
  @content
end

#fileObject

Returns the value of attribute file.



3
4
5
# File 'lib/plate/layout.rb', line 3

def file
  @file
end

#metaObject

Returns the value of attribute meta.



3
4
5
# File 'lib/plate/layout.rb', line 3

def meta
  @meta
end

#siteObject

Returns the value of attribute site.



3
4
5
# File 'lib/plate/layout.rb', line 3

def site
  @site
end

Instance Method Details

#<=>(other) ⇒ Object

Compare two layouts, by name.



139
140
141
# File 'lib/plate/layout.rb', line 139

def <=>(other)
  self.name <=> other.name
end

#==(other) ⇒ Object

Is this layout equal to another page being sent?



133
134
135
136
# File 'lib/plate/layout.rb', line 133

def ==(other)
  other = other.relative_file if other.respond_to?(:relative_file)
  self.id == other or self.relative_file == other
end

#basenameObject

The name of the layound, without any path data



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

def basename
  File.basename(self.file)
end

#default?Boolean

Is this layout the default layout, by name.

Returns:

  • (Boolean)


20
21
22
# File 'lib/plate/layout.rb', line 20

def default?
  self.name.downcase.strip.start_with? "default"
end

#engineObject

The layout engine to use. Based off of the last file extension for this layout.



25
26
27
# File 'lib/plate/layout.rb', line 25

def engine
  @engine ||= self.site.registered_page_engines[self.extension.gsub(/\./, '').to_sym]
end

#extensionObject

The last file extension of this layout.



30
31
32
# File 'lib/plate/layout.rb', line 30

def extension
  File.extname(self.file)
end

#file?Boolean

Does the file exist or not.

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/plate/layout.rb', line 35

def file?
  return false if self.file.nil?
  File.exists?(self.file)
end

#idObject

A unique ID for this layout.



41
42
43
# File 'lib/plate/layout.rb', line 41

def id
  @id ||= Digest::MD5.hexdigest(relative_file)
end

#inspectObject



45
46
47
# File 'lib/plate/layout.rb', line 45

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} name=#{name.to_s.inspect}>"
end

#load!Object

Raises:



49
50
51
52
53
54
55
56
57
# File 'lib/plate/layout.rb', line 49

def load!
  return if @loaded
  raise FileNotFound unless file?

  read_file!
  read_metadata!

  @loaded = true
end

#nameObject

The name for a layout is just the lowercase, first part of the file name.



60
61
62
63
# File 'lib/plate/layout.rb', line 60

def name
  return "" unless file?
  @name ||= self.basename.to_s.downcase.strip.split('.')[0]
end

#parentObject

A parent layout for this current layout file. If no layout is specified for this layout’s parent, then nil is returned. If there is a parent layout for this layout, any pages using it will be rendered using this layout first, then sent to the parent for further rendering.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/plate/layout.rb', line 69

def parent
  return @parent if @parent

  if self.meta[:layout]
    @parent = self.site.find_layout(self.meta[:layout])
  else
    @parent = nil
  end

  @parent
end

#relative_fileObject



81
82
83
# File 'lib/plate/layout.rb', line 81

def relative_file
  @relative_file ||= self.site.relative_path(self.file)
end

#reload!Object



85
86
87
88
89
90
91
# File 'lib/plate/layout.rb', line 85

def reload!
  @template = nil
  @loaded = false
  @name = nil
  @engine = nil
  load!
end

#render(content, page = nil, view = nil) ⇒ Object

Render the given content against the current layout template.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/plate/layout.rb', line 94

def render(content, page = nil, view = nil)
  if self.template
    view ||= View.new(self.site, page)
    result = self.template.render(view) { content }

    if self.parent
      result = self.parent.render(result, page, view)
    end

    view = nil

    result
  else
    content.respond_to?(:rendered_content) ? content.rendered_content : content.to_s
  end
end

#templateObject

The render template to use for this layout. A template is only used if the file extension for the layout is a valid layout extension from the current site.



114
115
116
117
118
119
120
121
122
# File 'lib/plate/layout.rb', line 114

def template
  return @template if @template

  if template?
    @template = self.engine.new() { self.content }
  else
    nil
  end
end

#template?Boolean

Does this file have the ability to be used as a template?

This currently only works if the layout is a .erb file. Otherwise anything that calls this layout just returns the text it is given.

Returns:

  • (Boolean)


128
129
130
# File 'lib/plate/layout.rb', line 128

def template?
  self.site.page_engine_extensions.include?(self.extension)
end