Templates if/else

8,003 views
Skip to first unread message

Gary Richardson

unread,
Oct 15, 2008, 8:24:34 PM10/15/08
to puppet...@googlegroups.com
Hey,

I'd like to do something like this in my template:

<%= if has_variable?('some_variable') then %><%= some_variable%><% else %>default<% end %>

http://reductivelabs.com/trac/puppet/wiki/PuppetTemplating talks about conditionals, but not so much about if/else.

Any suggestions?

Thanks!

Andrew Shafer

unread,
Oct 15, 2008, 9:05:28 PM10/15/08
to puppet...@googlegroups.com

Gary,

The templates are using standard ERB, which essentially gives you the ability to do arbitrary conditional logic using ruby.

Anything between <% %> is logic code and <%= %> is evaluated and printed.

<% if condition %>
<%= print_this %>

<% else %>
default
<% end %>

Play with ERB, it is very flexible.

Gary Richardson

unread,
Oct 16, 2008, 4:20:45 PM10/16/08
to puppet...@googlegroups.com
Thanks, that got it going for me. I don't know if there was a typo in my block of code, but it didn't work for the first time around.

Andrew Shafer

unread,
Oct 16, 2008, 5:33:33 PM10/16/08
to puppet...@googlegroups.com

I think your original snippet doesn't work because the first if is inside an <%= %> block.  So the rest of the code probably doesn't parse right.
Reply all
Reply to author
Forward
0 new messages