2012-03-23 2 views
7

は、私はこのような何かをしたい:非レールアプリでerbパーシャルを実装するにはどうすればいいですか?

require 'erb' 
@var = 'test' 
template = ERB.new File.new("template.erb").read 
rendered = template.result(binding()) 

しかし、どのように私はtemplate.erbでパーシャルを使用することができますか?

+0

可能な複製http://stackoverflow.com/a/2467313/772874それには 'ActionView'が必要です。 –

答えて

6

おそらくブルートフォースですか?

header_partial = ERB.new(File.new("header_partial.erb").read).result(binding) 
footer_partial = ERB.new(File.new("footer_partial.erb").read).result(binding) 

template = ERB.new <<-EOF 
    <%= header_partial %> 
    Body content... 
    <%= footer_partial %> 
EOF 
puts template.result(binding) 
+0

ありがとう!それはまさに私が思いついたものです) – bluegray

+0

それに役立つ宝石はありますか? – Kirby

1

は(別名、別々の結果は、レンダリング同じことを見つけるためにしようとしていたとERBや他のテンプレートシステムをラップし、ブロックを渡すサポートTilt gemを使用するよりも、他の満足している多くを見つけることができませんでしたコール)。これは少し良いかもしれません。上で見

https://code.tutsplus.com/tutorials/ruby-for-newbies-the-tilt-gem--net-20027

layout.erb

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8" /> 
    <title><%= title %></title> 
</head> 
<body> 
    <%= yield %> 
</body> 
</html> 

その後、あなたのルビーコール

template = Tilt::ERBTemplate.new("layout.erb") 

File.open "other_template.html" do |file| 
    file.write template.render(context) { 
     Tilt::ERBTemplate.new("other_template.erb").render 
    } 
end 

にそれはyield体内にother_templateの結果を適用します。

関連する問題