ruby-on-rails - 没有 rails 的 ERB 中的 yield

标签 ruby-on-rails ruby erb

如何在 erb 中使用 yield 进行模板继承?我想在一个普通的 ruby​​ CGI 脚本中使用 erb,并想像 Rails 一样使用基本模板和子模板以及应用程序模板。

最佳答案

def a
  ERB.new('<%= yield %>').result(binding)
end
a{123}
#=> "123"

在方法内部调用 Kernel#binding 很重要,这样上下文就包含 block (ERB#result 不会占用 block )。

关于ruby-on-rails - 没有 rails 的 ERB 中的 yield ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2460839/

相关文章:

ruby-on-rails - 将 Rails 应用程序连接到 Firebase Cloud Firestore

ruby - rspec 是否有比 target.should < 6 更具体的内容?

ruby-on-rails - 在 textmate 中自动格式化 ERB 模板

javascript - 如何动态创建包含类属性的列表

ruby-on-rails - 如何在 rails View 中显示数组?

mysql - 我正在使用 Ruby on Rails,我想删除一个数据库条目。我可以将条目设为 "cut"并将所有较大的 ID 减一吗?

ruby - 为什么 collections.find({}) 为 250 个对象占用 9 秒以上 (MongoMapper)

ruby-on-rails - 从 ActiveRecord 模型集合构建散列

ruby-on-rails - :remote => true not working when a file uploader is used in form

ruby-on-rails - 如何将 Sinatra erb 模板 View 结果加载到变量中?是否有 render_to_string 等价物?