Using arrays in templates

Hi,

I have a question about templates and the correct syntax.
I have an array which contains the following data:

“name1”,"description of name1"
“name2”,“description of name2”

“nameN”,“description of nameN”

This array needs to be stored as an attribute of a role or node (so
that it can be overridden)

I assume it could look something like this:

“host”:[[“name1”,“description of name1”],[“name2”,“description of name2”]]

Now the problem (at least for me) is how do I specify this in the template?

I have tried this (I assume it does not work because it needs to have
a object and not array):

<% @host.each do |h| %>
<%= host[0] %> <%= host[1]%>
<% end %>

if I write something like this
<% for h in @hosts %>
<%= h[0] %> <%= h[1]%>
<% end %>
the I get render_template': undefined methodkeys’ for
#Array:0x2b099b406648 (Chef::Mixin::Template::TemplateError)

So my question is, how can I use arrays in templates and store their
values in a role or node attribute?
Can you show me an example of this?

Thanks a lot!

Regards,
chef user

On Wed, Jul 28, 2010 at 4:50 AM, FX lists@bgp.lv wrote:

Hi,

I have a question about templates and the correct syntax.
I have an array which contains the following data:

"name1","description of name1"
"name2","description of name2"
...
"nameN","description of nameN"

This array needs to be stored as an attribute of a role or node (so
that it can be overridden)

I assume it could look something like this:

"host":[["name1","description of name1"],["name2","description of name2"]]

This should be a hash, not an array - you have a "name" value, which
should be the key, and the descirption is a value.

Now the problem (at least for me) is how do I specify this in the template?

I have tried this (I assume it does not work because it needs to have
a object and not array):

<% @host.each do |h| %>
<%= host[0] %> <%= host[1]%>
<% end %>

This is the right syntax - you need to have <%= h[0] %> and <%= h[1]
%>, since that's the variable you create on iteration.

Adam

Opscode, Inc.
Adam Jacob, CTO
T: (206) 508-7449 E: adam@opscode.com

I'm no ruby expert, but It's worth pointing out that ordering of a Hash is
undefined. So the array-in-array approach works well if you intend to have
exact order in and out... which you likely do.

Perhaps someone with more ruby knowledge can demonstrate a ordered hash.

~ Sig

On Thu, Jul 29, 2010 at 3:51 PM, Adam Jacob adam@opscode.com wrote:

On Wed, Jul 28, 2010 at 4:50 AM, FX lists@bgp.lv wrote:

Hi,

I have a question about templates and the correct syntax.
I have an array which contains the following data:

"name1","description of name1"
"name2","description of name2"
...
"nameN","description of nameN"

This array needs to be stored as an attribute of a role or node (so
that it can be overridden)

I assume it could look something like this:

"host":[["name1","description of name1"],["name2","description of
name2"]]

This should be a hash, not an array - you have a "name" value, which
should be the key, and the descirption is a value.

Now the problem (at least for me) is how do I specify this in the
template?

I have tried this (I assume it does not work because it needs to have
a object and not array):

<% @host.each do |h| %>
<%= host[0] %> <%= host[1]%>
<% end %>

This is the right syntax - you need to have <%= h[0] %> and <%= h[1]
%>, since that's the variable you create on iteration.

Adam

Opscode, Inc.
Adam Jacob, CTO
T: (206) 508-7449 E: adam@opscode.com

Hi,
Look over here

http://dreamcat4.github.com/plist4r/frames.html?http://dreamcat4.github.com/plist4r/Plist4r/OrderedHash.html

On Sat, Jul 31, 2010 at 3:20 AM, Sig Lange sig.lange@gmail.com wrote:

I'm no ruby expert, but It's worth pointing out that ordering of a Hash is
undefined. So the array-in-array approach works well if you intend to have
exact order in and out... which you likely do.

Perhaps someone with more ruby knowledge can demonstrate a ordered hash.

~ Sig

On Thu, Jul 29, 2010 at 3:51 PM, Adam Jacob adam@opscode.com wrote:

On Wed, Jul 28, 2010 at 4:50 AM, FX lists@bgp.lv wrote:

Hi,

I have a question about templates and the correct syntax.
I have an array which contains the following data:

"name1","description of name1"
"name2","description of name2"
...
"nameN","description of nameN"

This array needs to be stored as an attribute of a role or node (so
that it can be overridden)

I assume it could look something like this:

"host":[["name1","description of name1"],["name2","description of
name2"]]

This should be a hash, not an array - you have a "name" value, which
should be the key, and the descirption is a value.

Now the problem (at least for me) is how do I specify this in the
template?

I have tried this (I assume it does not work because it needs to have
a object and not array):

<% @host.each do |h| %>
<%= host[0] %> <%= host[1]%>
<% end %>

This is the right syntax - you need to have <%= h[0] %> and <%= h[1]
%>, since that's the variable you create on iteration.

Adam

Opscode, Inc.
Adam Jacob, CTO
T: (206) 508-7449 E: adam@opscode.com

You can also sort the hash on the way out, as well.

:slight_smile:

Adam

On Fri, Jul 30, 2010 at 7:20 PM, Sig Lange sig.lange@gmail.com wrote:

I'm no ruby expert, but It's worth pointing out that ordering of a Hash is
undefined. So the array-in-array approach works well if you intend to have
exact order in and out... which you likely do.

Perhaps someone with more ruby knowledge can demonstrate a ordered hash.

~ Sig

On Thu, Jul 29, 2010 at 3:51 PM, Adam Jacob adam@opscode.com wrote:

On Wed, Jul 28, 2010 at 4:50 AM, FX lists@bgp.lv wrote:

Hi,

I have a question about templates and the correct syntax.
I have an array which contains the following data:

"name1","description of name1"
"name2","description of name2"
...
"nameN","description of nameN"

This array needs to be stored as an attribute of a role or node (so
that it can be overridden)

I assume it could look something like this:

"host":[["name1","description of name1"],["name2","description of
name2"]]

This should be a hash, not an array - you have a "name" value, which
should be the key, and the descirption is a value.

Now the problem (at least for me) is how do I specify this in the
template?

I have tried this (I assume it does not work because it needs to have
a object and not array):

<% @host.each do |h| %>
<%= host[0] %> <%= host[1]%>
<% end %>

This is the right syntax - you need to have <%= h[0] %> and <%= h[1]
%>, since that's the variable you create on iteration.

Adam

Opscode, Inc.
Adam Jacob, CTO
T: (206) 508-7449 E: adam@opscode.com

--
Opscode, Inc.
Adam Jacob, CTO
T: (206) 508-7449 E: adam@opscode.com

Thanks a lot for your answers guys!

On Sun, Aug 1, 2010 at 12:53 AM, Adam Jacob adam@opscode.com wrote:

You can also sort the hash on the way out, as well.

:slight_smile:

Adam

On Fri, Jul 30, 2010 at 7:20 PM, Sig Lange sig.lange@gmail.com wrote:

I'm no ruby expert, but It's worth pointing out that ordering of a Hash is
undefined. So the array-in-array approach works well if you intend to have
exact order in and out... which you likely do.

Perhaps someone with more ruby knowledge can demonstrate a ordered hash.

~ Sig

On Thu, Jul 29, 2010 at 3:51 PM, Adam Jacob adam@opscode.com wrote:

On Wed, Jul 28, 2010 at 4:50 AM, FX lists@bgp.lv wrote:

Hi,

I have a question about templates and the correct syntax.
I have an array which contains the following data:

"name1","description of name1"
"name2","description of name2"
...
"nameN","description of nameN"

This array needs to be stored as an attribute of a role or node (so
that it can be overridden)

I assume it could look something like this:

"host":[["name1","description of name1"],["name2","description of
name2"]]

This should be a hash, not an array - you have a "name" value, which
should be the key, and the descirption is a value.

Now the problem (at least for me) is how do I specify this in the
template?

I have tried this (I assume it does not work because it needs to have
a object and not array):

<% @host.each do |h| %>
<%= host[0] %> <%= host[1]%>
<% end %>

This is the right syntax - you need to have <%= h[0] %> and <%= h[1]
%>, since that's the variable you create on iteration.

Adam

Opscode, Inc.
Adam Jacob, CTO
T: (206) 508-7449 E: adam@opscode.com

--
Opscode, Inc.
Adam Jacob, CTO
T: (206) 508-7449 E: adam@opscode.com