Class: Yuzu::Core::Layout

Inherits:
HamlTemplate show all
Defined in:
lib/yuzu/core/layout.rb

Overview

A Layout is a Template that lays out an entire page. It consists of a collection of partial templates, namely for the HTML <head>, but also for a page header, footer, and navigation menu. In the future, this will be generalized.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HamlTemplate

#engine, #locals, #options

Methods inherited from Template

#contents, #exists?, #fallback_exists?, #fallback_path, #get_contents, #get_template_contents, #initialize, #path

Constructor Details

This class inherits a constructor from Yuzu::Core::Template

Class Method Details

.get_partialsObject

Calculates the Hash for self.partials.



30
31
32
33
34
35
36
37
# File 'lib/yuzu/core/layout.rb', line 30

def self.get_partials
  tr = {}
  partial_filenames.each do |filename|
    partial_name = filename[1...-5]
    tr[partial_name] = HamlTemplate.new(filename)
  end
  tr
end

.partial_filenamesHash

A hash of partial name to filename.

Returns:

  • (Hash)


16
17
18
19
20
# File 'lib/yuzu/core/layout.rb', line 16

def self.partial_filenames
  relative_paths = Dir[(@@template_dir + "*").to_s]
  files = relative_paths.collect {|p| File.basename(p)}
  files.select {|filename| filename[0].chr == "_"}
end

.partialsHash

A hash of partial name to the corresponding HamlTemplate object for that partial.

Returns:

  • (Hash)

    of String => HamlTemplate



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

def self.partials
  @@partials ||= get_partials
end

Instance Method Details

#layout_locals(website_file) ⇒ Hash

A Hash of all the local variables passed into the Haml engine when the layout is rendered.

Parameters:

  • website_file (WebsiteFile)

    The website file being rendered.

Returns:

  • (Hash)

    of local variables available in the template.



43
44
45
# File 'lib/yuzu/core/layout.rb', line 43

def layout_locals(website_file)
  {:layout => PageLayout.new(website_file, Layout.partials)}
end

#render(website_file) ⇒ String

Actually renders the layout for the given file.

Parameters:

  • website_file (WebsiteFile)

    The website_file being rendered in the layout.

Returns:

  • (String)

    A string containing the HTML rendered contents of the file as a webpage.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/yuzu/core/layout.rb', line 51

def render(website_file)
  # Contains the standard local variables `post` and `config`.
  local_variables = locals(website_file)

  # Add `layout` to the mix.
  layout_local_variables = layout_locals(website_file)

  engine.render(
    Yuzu::Core::TemplateMethods.new(website_file.root),
    local_variables.merge(layout_local_variables)
  )
end