简体   繁体   English

如何将数组传递给 ruby 中的 erb 模板并对其进行迭代?

[英]How do you pass an array to an erb template in ruby and have it iterated over?

I need some help with erb templates, I can't seem to get my head around passing an array and then iterating over it.我需要一些有关 erb 模板的帮助,我似乎无法理解传递一个数组然后对其进行迭代。 My problem is this.我的问题是这个。 I want to pass a few arrays: `我想通过几个arrays:`

device      => ["eth0", "br0"],
ipaddr      => ["192.168.12.166", "192.168.12.199"],
netmask     => ["255.255.255.0", "255.255.255.0"], 
hwaddr      => '',
network     => '',
gateway     => ["192.168.12.254", "192.168.12.204"],                                                                                                                

To a template that iterates over each item in the array and prints it out:对于迭代数组中的每个项目并将其打印出来的模板:

auto <%= device %> inet static                                                                                                                                        
address <%= ipaddr %>
netmask <%= netmask %>
broadcast <%= broadcast %>
gateway <%= gateway %>

As far as I can get so far is figuring out that I need to do something with device.each |device| puts device据我所知,我需要对device.each |device| puts device做一些事情。 device.each |device| puts device , but I don't know what the syntax is supposed to look like. device.each |device| puts device ,但我不知道语法应该是什么样子。 I believe you can tell what I'm trying to do from these snippets, and then you might understand that the entries need to be seperate, and not interpolated.我相信你可以从这些片段中看出我想要做什么,然后你可能会明白这些条目需要分开,而不是插值。 Any help you can offer would be appreciated.您可以提供的任何帮助将不胜感激。 I know I should be trying things out in irb and figuring them out from there, which is what I'm reading up on now.我知道我应该在 irb 中尝试并从那里找出它们,这就是我现在正在阅读的内容。

Thanks!谢谢!

the basic syntax for using each in ruby is something like this:在 ruby 中使用 each 的基本语法是这样的:

array.each do |item_from_array| BLOCK

so if you only had one array then you could just do something like this: (I would use a different name inside the vertical bars for clarity)所以如果你只有一个数组,那么你可以这样做:(为了清楚起见,我会在垂直条内使用不同的名称)

<% device.each do |dev| %>
  auto <%= dev %> inet static
<% end %>

However that would iterate over all of your devices first, before moving on to your ipaddr array.但是,这将首先遍历您的所有设备,然后再转到您的 ipaddr 数组。 I'm guessing you want them each in turn auto, address, netmask, etc. In that case you'd be better off using a more 'traditional' index and looping through N times, like this:我猜您希望它们依次自动、地址、网络掩码等。在这种情况下,您最好使用更“传统”的索引并循环 N 次,如下所示:

<% for idx in (0..1) %>
  auto <%= device[idx] %> inet static
  address <%= address[idx] %>
  netmask <%= netmask[idx] %>
  broadcast <%= broadcast[idx] %>
<% end %>

Of course you need to think about what your maximum size of array is, and what to do if an array contains less entries than the others.当然,您需要考虑数组的最大大小是多少,以及如果数组包含的条目比其他数组少,该怎么办。 You can find the maximum size of all the arrays by doing something like this: [device,address,netmask,broadcast].map{|a| a.length}.max您可以通过执行以下操作找到所有 arrays 的最大大小: [device,address,netmask,broadcast].map{|a| a.length}.max [device,address,netmask,broadcast].map{|a| a.length}.max

and you can skip over a particular array like this: <% if idx < address.length %> address <%= address[idx] %><% end %>你可以像这样跳过一个特定的数组: <% if idx < address.length %> address <%= address[idx] %><% end %>

ERB Templates for Dummies傻瓜的 ERB 模板

Basic code:基本代码:

require 'erb'
ERB.new(template).result binding_for_template

What are template and binding_for_template ?什么是templatebinding_for_template

Template模板

Just the template content.只是模板内容。 You can read it from a file just with a File.read(path) .您可以使用File.read(path)从文件中读取它。

Binding for template模板绑定

A binding绑定

encapsulate the execution context at some particular place in the code and retain this context for future use.将执行上下文封装在代码中的某个特定位置,并保留此上下文以供将来使用。

How do you use it?你如何使用它? Easy:简单的:

def binding_for_my_template
  devices      = ["eth0", "br0"]
  ipaddrs      = ["192.168.12.166", "192.168.12.199"]
  netmasks     = ["255.255.255.0", "255.255.255.0"]
  hwaddrs      = ['']
  networks     = ['']
  gateways     = ["192.168.12.254", "192.168.12.204"]
  binding
end

That will return a binding with all six arrays (I changed hwaddr and network to arrays for consistency.这将返回与所有六个 arrays 的绑定(我将hwaddrnetwork更改为 arrays 以保持一致性。

Iterating over arrays迭代 arrays

The usual way is using the each method, just like this:通常的方法是使用each方法,就像这样:

<%- devices.each do |d| %>
  auto <%= d %> inet static
<%- end %>

but there are other methods if you wanna put all in one line, for example devices.join ' ' will join all the strings with a space in between.但是如果您想将所有内容放在一行中,还有其他方法,例如devices.join ' '将连接所有字符串,其间有空格。

You should read the docs .您应该阅读文档 Quoting:报价:

# Managed by Class['ntp']
<% servers_real.each do |server| -%>
server <%= server %>
<% end -%>

# ...

This snippet will iterate over each entry in the array and print it after a server statement, so, for example, the string generated from the Debian template will end up with a block like this:此代码段将遍历数组中的每个条目并在服务器语句之后打印它,因此,例如,从 Debian 模板生成的字符串将以这样的块结束:

# Managed by Class['ntp']
server 0.debian.pool.ntp.org iburst
server 1.debian.pool.ntp.org iburst
server 2.debian.pool.ntp.org iburst
server 3.debian.pool.ntp.org iburst

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM