Conditional in ERB template

31 views
Skip to first unread message

Jonathan Gazeley

unread,
Feb 9, 2017, 8:30:30 AM2/9/17
to puppet...@googlegroups.com
Hi folks,

I'm using an ERB template to write out a chunk of config file. The key
line of the template is this:

members=<% @members.each_pair do |key, value| -%><%= key -%>;<%=
value['service'] -%>;<%= value['opt'] -%>, <% end %>

and it produces this output:

members=radius-local01.example.com;RADIUS;&,
radius-local02.example.com;RADIUS;&, radius-local03.example.com;RADIUS;&,

However, both the 'service' and 'opt' tags are optional so I wrapped
them in conditionals like this:

members=<% @members.each_pair do |key, value| -%><%= key -%><% if
value['service'] %>;<%= value['service'] %><% end %><% if value['opt']
%>;<%= value['opt']%><% end %>, <% end %>

But this resulted in value['service'] being printed as a literal string.
What have I done wrong?

members=db1.example.com;value['service'];value['opt'],db2.example.com;value['service'];value['opt'],db3.example.com;value['service'];value['opt'],

Any hints would be appreciated!

Thanks,
Jonathan

--
Jonathan Gazeley
Senior Systems Administrator
IT Services
University of Bristol
Reply all
Reply to author
Forward
0 new messages