I'm trying to convert erb templates to epp (new company policy) and there isn't a lot of documentation on epp yet.
Here's what i have in erb:
<% filter.select{|x| x != 'filtertype'}.sort.each do |key, element| -%> <%= key %>: '<%= element %>' <% end -%> it works great! however i have to find the equivalent for epp. I can get the "each" part to work, but the select method isn't working for me.
I'm stumped!
I tried something like:
<% $filter.select { |$x| $x != 'filtertype'}.each |$key, $element| { -%> <%= $key %>: '<%= $element %>' <% } -%> this in particular errors on '|' for the $x.
I also tried:
<% $filter.select |$x| {$x != 'filtertype'}.each |$key, $element| { -%> <%= $key %>: '<%= $element %>' <% } -%> but that gives me something like "Error while evaluating a Method call, select(): Wrong number of arguments given 1 for 3"
I've tried moving around the {} and changing them to (), but no luck.
does anyone have any ideas?
Thanks!
21 Answer
As Dominic Cleal mentions in the comment, select() is not a Puppet function, however filter() is and is the equivalent of select in Ruby.
Therefore:
Given a class:
class foo () { # Some test data. $filter = { 'filtertype' => 'foo', 'apples' => 1, 'bananas' => 2, } # How to declare the ERB template for comparison: file { '/foo': ensure => file, content => template('foo/mytemplate.erb'), } # How to declare the EPP template for comparison: file { '/bar': ensure => file, content => epp('foo/mytemplate.epp', {'filter' => $filter}), } } Content of the ERB file exactly as given in the question:
<% @filter.select{|x| x != 'filtertype'}.sort.each do |key, element| -%> <%= key %>: '<%= element %>' <% end -%> Content of an equivalent EPP file:
<% | Hash $filter | -%> <% include stdlib -%> <% $filter.keys.sort.filter |$k| {$k != 'filtertype'}.each |$k| { -%> <%= $k %>: '<%= $filter[$k] %>' <% } -%> Things to note:
1) You need include stdlib to access the keys and sort functions.
2) The variable name $filter now clashes with the built-in Puppet function filter(), which is syntactically fine, but confusing, so you would consider renaming the $filter variable to something else for clarity.
Also, if you don't care about sorting the keys, then I could make what you tried work using:
<% | Hash $filter | -%> <% $filter.filter |$k| {$k[0] != 'filtertype'}.each |$k, $v| { -%> <%= $k %>: '<%= $v %>' <% } -%> See also here where I just answered a similar question.