开发者

Is there a way to make a regular ruby class use a erb/haml template?

开发者 https://www.devze.com 2023-03-25 09:32 出处:网络
Basically, I have a class that outputs some html: class Foo include ActionView::Helpers def initialize(stuff)

Basically, I have a class that outputs some html:

class Foo
  include ActionView::Helpers

  def initialize(stuff)
    @stuff = s开发者_JS百科tuff
  end

  def bar
    content_tag :p, @stuff
  end

end

so I can do: Foo.new(123).bar

and get "<p>123</p>"

... But what I really want to do is something like this:

class Foo << ActionView::Base

  def initialize(stuff)
    @stuff = stuff
  end

  def bar
    render :template => "#{Rails.root/views/foo/omg.html.erb}"
  end

end

# views/omg.html.erb

<h1>Wow, stuff is <%= @stuff %></h1>

and then do Foo.new(456).bar and get "<h1>Wow, stuff is 456</h1>"


Just call erb directly? Something like:

def bar
  template = ERB.new(File.read("#{Rails.root}/views/foo/omg.html.erb"))
  template.result(binding)
end
0

精彩评论

暂无评论...
验证码 换一张
取 消