Class: Chisel::ViewHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/chisel/view_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site_dir, output_path) ⇒ ViewHelper



5
6
7
8
9
# File 'lib/chisel/view_helper.rb', line 5

def initialize(site_dir, output_path)
  @site_dir = site_dir
  @config = @site_dir.config
  @output_path = output_path
end

Instance Attribute Details

#output_pathObject

Returns the value of attribute output_path.



3
4
5
# File 'lib/chisel/view_helper.rb', line 3

def output_path
  @output_path
end

#site_dirObject

Returns the value of attribute site_dir.



3
4
5
# File 'lib/chisel/view_helper.rb', line 3

def site_dir
  @site_dir
end

Instance Method Details



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/chisel/view_helper.rb', line 48

def link_to(href, text, options = {})
  if options[:html]
    attribute_string = ''
    options[:html].each do |k, v| 
      attribute_string += " #{k}=\"#{v}\"" if LinkAttributes.index(k.to_s.downcase) 
    end
  end

  href = href[0..-11] if href.end_with?('index.html') and href != 'index.html'

  "<a href=\"#{href}\"#{attribute_string}>#{text}</a>"
end


37
38
39
40
41
42
# File 'lib/chisel/view_helper.rb', line 37

def link_to_page(page, text, options = {})      
  page_output_path = @site_dir.page_output_path(page, @output_path.dirname)
  href = page_output_path.relative_path_from(@output_path.dirname).to_s

  link_to(href, text, options)
end


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/chisel/view_helper.rb', line 19

def link_to_resource(resource, text, options = {})
  view = options[:view] || 'index'

  view_path = @site_dir.resource_view_output_path(resource, view)
  href = view_path.relative_path_from(@output_path.dirname).to_s

  # Initialize the view template
  template = View.fetch(:resource => resource.resource_type, :view => view, :site_dir => @site_dir)

  # Compute the output path for this specific resource
  output_path = @site_dir.resource_view_output_path(resource, view)

  # Now run the view template against this resource
  template.run(:output_path => output_path, :resource => resource)

  link_to(href, text, options)
end

#path_to(site_relative_path) ⇒ Object



44
45
46
# File 'lib/chisel/view_helper.rb', line 44

def path_to(site_relative_path)
  @site_dir.site_relative_path(site_relative_path, @output_path.dirname).relative_path_from(@output_path.dirname)
end

#render(view, options = {}) ⇒ Object



11
12
13
14
15
16
# File 'lib/chisel/view_helper.rb', line 11

def render(view, options = {})
  locals = options[:locals] || {}

  template = View.fetch(:view => view, :site_dir => @site_dir)
  template.evaluate(@output_path, :locals => locals)
end