Class: Plato::RenderContext

Inherits:
Object
  • Object
show all
Includes:
Tilt::CompileSite
Defined in:
lib/plato/rendering.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, document) ⇒ RenderContext

Returns a new instance of RenderContext.



8
9
10
11
# File 'lib/plato/rendering.rb', line 8

def initialize(site, document)
  @site = site
  @document = document
end

Instance Attribute Details

#documentObject (readonly) Also known as: post

Returns the value of attribute document.



5
6
7
# File 'lib/plato/rendering.rb', line 5

def document
  @document
end

#siteObject (readonly)

Returns the value of attribute site.



5
6
7
# File 'lib/plato/rendering.rb', line 5

def site
  @site
end

Class Method Details

.load_view_helpers(path) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/plato/rendering.rb', line 38

def self.load_view_helpers(path)
  return if @helpers_loaded
  @helpers_loaded = true

  mod = Module.new
  mod.module_eval(File.read(path), path, 1)
  include mod.const_get(mod.constants.first)
end

Instance Method Details

#attribute_pairs(hash) ⇒ Object



61
62
63
# File 'lib/plato/rendering.rb', line 61

def attribute_pairs(hash)
  hash.map {|k,v| %{#{k}="#{v}"} }.join(' ')
end

#contentObject



59
# File 'lib/plato/rendering.rb', line 59

def content; site.content end

#css_include(url, opts = {}) ⇒ Object



65
66
67
68
69
# File 'lib/plato/rendering.rb', line 65

def css_include(url, opts = {})
  url = "#{url.gsub(/\.css\Z/, '')}.css"
  opts = opts.merge :type => "text/css", :rel => "stylesheet", :href=> url_for(url)
  %{<link #{attribute_pairs(opts)} />}
end


55
56
57
# File 'lib/plato/rendering.rb', line 55

def link_to(title, url)
  %{<a href="#{url}">#{title}</a>}
end

#render(template, locals = {}, &block) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
# File 'lib/plato/rendering.rb', line 17

def render(template, locals = {}, &block)
  template = self.template(template) if template.is_a? String
  raise ArgumentError, "template #{template.inspect} not found" unless template
  template.render(self, locals, &block)
end

#render_bodyObject Also known as: body



33
34
35
# File 'lib/plato/rendering.rb', line 33

def render_body
  document.body(self)
end

#render_with_layout(template, format = nil, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/plato/rendering.rb', line 23

def render_with_layout(template, format = nil, &block)
  layout = template("_layout.#{format}") if format

  if layout
    render(layout) { render(template, &block) }
  else
    render(template, &block)
  end
end

#script_include(url, opts = {}) ⇒ Object



71
72
73
74
75
# File 'lib/plato/rendering.rb', line 71

def script_include(url, opts = {})
  url = "#{url.gsub(/\.js\Z/, '')}.js"
  opts = opts.merge :src => url_for(url)
  %{<script #{attribute_pairs(opts)}></script>}
end

#template(key) ⇒ Object



13
14
15
# File 'lib/plato/rendering.rb', line 13

def template(key)
  site.templates["#{key}.html"] || site.templates[key]
end

#url_for(doc, opts = {}) ⇒ Object

base set of helpers



49
50
51
52
53
# File 'lib/plato/rendering.rb', line 49

def url_for(doc, opts = {})
  return doc if doc.is_a? String and doc =~ /\Ahttp:\/\//
  base = opts[:absolute] ? site.base_url : '/'
  File.join(base, doc.respond_to?(:path) ? doc.path : doc.to_s)
end