如何将数组传递给ruby中的erb模板,并遍历它?

时间:2021-11-05 22:30:41

I need some help with erb templates, I can't seem to get my head around passing an array and then iterating over it. My problem is this. I want to pass a few arrays: `

我需要一些erb模板的帮助,我似乎不能在传递一个数组然后在它上面迭代。我的问题是这样的。我想传递一些数组:

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, but I don't know what the syntax is supposed to look like. 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!

谢谢!

3 个解决方案

#1


29  

the basic syntax for using each in ruby is something like this:

在ruby中使用它们的基本语法如下:

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. 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:

然而,这将首先遍历所有设备,然后再转到ipaddr数组。我猜你希望它们依次是自动的,地址,netmask等等。如果是这样的话,你最好使用一个更“传统”的索引,并循环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

当然,您需要考虑数组的最大大小,以及如果一个数组包含的条目比其他数组少,该怎么办。您可以通过以下操作找到所有数组的最大大小:[device,address,netmask,broadcast]。地图{ | | a.length } .max

and you can skip over a particular array like this: <% if idx < address.length %> address <%= address[idx] %><% end %>

您可以跳过这样的一个特定数组:<% if idx < address。长度%>地址<%=地址[idx] %><% end %>

#2


3  

ERB Templates for Dummies

Basic code:

基本代码:

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

What are template and binding_for_template?

什么是template和binding_for_template?

Template

Just the template content. You can read it from a file just with a 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.

这将返回与所有六个数组的绑定(为了一致性,我将hwaddr和network改为array)。

Iterating over arrays

The usual way is using the each method, just like this:

通常的方法是使用每一种方法,就像这样:

<%- 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.

但是如果你想把所有的都放到一行里,还有其他的方法,比如设备。连接' '将连接所有字符串,中间有空格。

#3


1  

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

#1


29  

the basic syntax for using each in ruby is something like this:

在ruby中使用它们的基本语法如下:

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. 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:

然而,这将首先遍历所有设备,然后再转到ipaddr数组。我猜你希望它们依次是自动的,地址,netmask等等。如果是这样的话,你最好使用一个更“传统”的索引,并循环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

当然,您需要考虑数组的最大大小,以及如果一个数组包含的条目比其他数组少,该怎么办。您可以通过以下操作找到所有数组的最大大小:[device,address,netmask,broadcast]。地图{ | | a.length } .max

and you can skip over a particular array like this: <% if idx < address.length %> address <%= address[idx] %><% end %>

您可以跳过这样的一个特定数组:<% if idx < address。长度%>地址<%=地址[idx] %><% end %>

#2


3  

ERB Templates for Dummies

Basic code:

基本代码:

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

What are template and binding_for_template?

什么是template和binding_for_template?

Template

Just the template content. You can read it from a file just with a 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.

这将返回与所有六个数组的绑定(为了一致性,我将hwaddr和network改为array)。

Iterating over arrays

The usual way is using the each method, just like this:

通常的方法是使用每一种方法,就像这样:

<%- 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.

但是如果你想把所有的都放到一行里,还有其他的方法,比如设备。连接' '将连接所有字符串,中间有空格。

#3


1  

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