Class: JewelryPortfolio::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/jewelry_portfolio/template.rb

Overview

This class is responsible for rendering a template.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, repos) ⇒ Template

Initialize with the path to the template, minus the extensions, and an array of JewelryPortfolio::Repo instances as repos.



17
18
19
20
21
22
# File 'lib/jewelry_portfolio/template.rb', line 17

def initialize(template, repos)
  template   = File.expand_path(template)
  @view_path = File.dirname(template)
  @template  = html_template_file(File.basename(template))
  @repos     = repos.to_a
end

Instance Attribute Details

#reposObject (readonly)

The array of JewelryPortfolio::Repo instances.



13
14
15
# File 'lib/jewelry_portfolio/template.rb', line 13

def repos
  @repos
end

#templateObject (readonly)

The full path to the template file.



7
8
9
# File 'lib/jewelry_portfolio/template.rb', line 7

def template
  @template
end

#view_pathObject (readonly)

The directory in which the template resides.



10
11
12
# File 'lib/jewelry_portfolio/template.rb', line 10

def view_path
  @view_path
end

Instance Method Details

#partial(partial_name, local_variables = {}) ⇒ Object

Renders a partial specified by partial_name minus the extension. You can optionally specify a hash of local_variables which will be available while rendering the partial.

Consider a partial named foo.html.erb, in the same directory as the template, containing the following:

Text: <%= text %>

This partial can now be rendered like this:

partial('foo', :text => 'bar') # => "Text: bar"


41
42
43
44
45
46
# File 'lib/jewelry_portfolio/template.rb', line 41

def partial(partial_name, local_variables = {})
  for (var_name, var_value) in local_variables
    eval "#{var_name} = var_value"
  end
  erb html_template_file(partial_name), binding
end

#renderObject

Renders the template and returns the output.



25
26
27
# File 'lib/jewelry_portfolio/template.rb', line 25

def render
  erb @template, binding
end

#repo_partial(repo, local_variables = {}) ⇒ Object

Renders a partial for the specified repo. This method looks for a partial file named repo.html.erb in the same directory as the template.

See partial for more info.



53
54
55
# File 'lib/jewelry_portfolio/template.rb', line 53

def repo_partial(repo, local_variables = {})
  partial 'repo', local_variables.merge(:repo => repo)
end