Class: JewelryPortfolio::Template
- Inherits:
-
Object
- Object
- JewelryPortfolio::Template
- Defined in:
- lib/jewelry_portfolio/template.rb
Overview
This class is responsible for rendering a template.
Instance Attribute Summary collapse
-
#repos ⇒ Object
readonly
The array of JewelryPortfolio::Repo instances.
-
#template ⇒ Object
readonly
The full path to the template file.
-
#view_path ⇒ Object
readonly
The directory in which the template resides.
Instance Method Summary collapse
-
#initialize(template, repos) ⇒ Template
constructor
Initialize with the path to the
template
, minus the extensions, and an array of JewelryPortfolio::Repo instances asrepos
. -
#partial(partial_name, local_variables = {}) ⇒ Object
Renders a partial specified by
partial_name
minus the extension. -
#render ⇒ Object
Renders the template and returns the output.
-
#repo_partial(repo, local_variables = {}) ⇒ Object
Renders a partial for the specified
repo
.
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.(template) @view_path = File.dirname(template) @template = html_template_file(File.basename(template)) @repos = repos.to_a end |
Instance Attribute Details
#repos ⇒ Object (readonly)
The array of JewelryPortfolio::Repo instances.
13 14 15 |
# File 'lib/jewelry_portfolio/template.rb', line 13 def repos @repos end |
#template ⇒ Object (readonly)
The full path to the template file.
7 8 9 |
# File 'lib/jewelry_portfolio/template.rb', line 7 def template @template end |
#view_path ⇒ Object (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 |
#render ⇒ Object
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 |