Module: Ramaze::Helper::Partial

Included in:
Ezamar::RenderPartial
Defined in:
lib/ramaze/helper/partial.rb

Overview

Helper::Partial

Example Usage

class MyController
  def index
  end
  def list
    plain = request['plain']
    "Hello World from List!  Plain List == #{plain}"
  end
end

<html>
  <head><title>Partial Render Index</title></head>
  <body>
    #{render_partial(Rs(:list), 'plain' => true)}
  </body>
</html>

Class Method Summary collapse

Class Method Details

.render_action(method, *params) ⇒ Object

shortcut to render_partial, accepts a method and contructs a link to the current controller, then calls render_partial on that



91
92
93
# File 'lib/ramaze/helper/partial.rb', line 91

def render_action method, *params
  render_partial(Rs(method), *params)
end

.render_partial(url, options = {}) ⇒ Object

Renders a url ‘inline’.

url: normal URL, like you’d use for redirecting. options: optional, will be used as request parameters.



35
36
37
38
39
40
41
42
43
44
# File 'lib/ramaze/helper/partial.rb', line 35

def render_partial(url, options = {})
  saved = {}
  options.keys.each {|x| saved[x] = Request.current.params[x] }

  Request.current.params.update(options)

  Controller.handle(url)
ensure
  options.keys.each {|x| Request.current.params[x] = saved[x] }
end

.render_template(file, vars = {}) ⇒ Object

Render the template file in view_root of the current controller.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ramaze/helper/partial.rb', line 49

def render_template(file, vars = {})
  current = Action.current
  options = { :controller => current.controller,
              :instance => current.instance.dup }

  file = file.to_s

  if Pathname(file).absolute?
    file = file.squeeze '/'
    unless File.exist?(file)
      Log.warn "render_template: #{file} does not exist."
      return ''
    end
    options[:template] = file
  else
    roots = [options[:controller].template_paths].flatten

    if (files = Dir["{#{roots.join(',')}}"/"{#{file},#{file}.*}"]).any?
      options[:template] = files.first.squeeze '/'
    else
      Log.warn "render_template: #{file} does not exist in the following directories: #{roots.join(',')}."
      return ''
    end
  end

  binding = options[:instance].scope

  vars.each do |name, value|
    options[:instance].instance_variable_set("@#{name}", value)

    value = "ObjectSpace._id2ref(#{ value.object_id })"
    eval "#{ name } = #{ value }", binding
  end

  options[:binding] = binding

  Ramaze::Action(options).render
end