Class: Plate::Partial

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

Overview

A partial is a special type of page that is not rendered on its own, but is called within another page. Just like partials in Rails, or server-side includes in other packages.

Instance Attribute Summary collapse

Attributes inherited from Page

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Page

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

Methods included from Callbacks

included

Constructor Details

#initialize(site, file) ⇒ Partial

Returns a new instance of Partial.



13
14
15
16
# File 'lib/plate/partial.rb', line 13

def initialize(site, file)
  @pages = []
  super(site, file)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Allow calls to locals hash via the locals proxy



37
38
39
40
# File 'lib/plate/partial.rb', line 37

def method_missing(method, *args)
  return super unless locals_proxy
  locals_proxy.send(method, *args)
end

Instance Attribute Details

#localsObject (readonly)

The locals hash provided to the current render of this partial. Locals are reset after each render



8
9
10
# File 'lib/plate/partial.rb', line 8

def locals
  @locals
end

#pagesObject

All pages that are using this partial, so they can be reloaded if the partial changes.



11
12
13
# File 'lib/plate/partial.rb', line 11

def pages
  @pages
end

Class Method Details

.find(site, name) ⇒ Object

Find a partial based on the given path and site



44
45
46
# File 'lib/plate/partial.rb', line 44

def find(site, name)
  site.partials.find { |partial| partial.name == name }
end

.name(site, file, reference = nil) ⇒ Object

Convert a file path into a partial name



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

def name(site, file, reference = nil)
  unless File.exists?(file)
    if (file =~ /^\.\.?\// or !file.include?('/')) and !reference.nil?
      file = File.expand_path(File.join(File.dirname(reference), file))
    else
      file = File.expand_path(File.join(site.source, 'content', file))
    end
  end

  base = File.basename(file).gsub(/^_/, '').split('.').first
  dir = site.relative_path(File.dirname(file))

  "#{dir}/#{base}".gsub(/^\/?content\//, '')
end

Instance Method Details

#nameObject

Partial names are the relative filename, without any extensions



19
20
21
# File 'lib/plate/partial.rb', line 19

def name
  @name ||= Partial.name(self.site, self.file, self.site.source)
end

#render(locals = {}) ⇒ Object

The content for the current partial using the given locals hash



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/plate/partial.rb', line 24

def render(locals = {})
  @locals = locals.symbolize_keys
  @locals_proxy = nil

  result = self.rendered_body
  @locals = nil
  @locals_proxy = nil
  @body = nil

  result
end