如何将不同的哈希对象转储到erb模板中

时间:2013-03-28 08:07:20

标签: ruby yaml erb

我有这样的erb模板,我想直接将哈希对象转储到它中。

# test.yml.erb
---
name: <%= name || "bat" %>
director_uuid: <%= uuid %>

release:
  name: bat
  version: <%= release.name || "latest" %>

<%= YAML.dump(compilation) %>

update:
  canaries: 1
  canary_watch_time: 3000-90000

<%= YAML.dump(network)%>

但我使用ERB.new(test.yml.erb).result(binding)只是得到这个:     #test.yml     ---     名字:蝙蝠     director_uuid:uuid

release:
  name: bat
  version: lastest

---
compliaton:
  workers: 1
  network: default    

update:
  canaries: 1
  canary_watch_time: 3000-90000

---
networks:
  name: default

有没有办法删除不需要的---? 任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

只需从YAML的转储输出中删除它:

<%= YAML.dump(compilation).sub(/.*?\n/, '') %>