How to increment variable in erb template ?

1,376 views
Skip to first unread message

AnOnJoe

unread,
Apr 24, 2012, 9:38:28 AM4/24/12
to puppet...@googlegroups.com
Hi',

I'm looking for a way to increment variable in template (erb file).

I tried this, but it didn't work

Compteur <%= compteur %>
<% compteur = compteur + 1 %>
Compteur <%= compteur %>

I've got this error :
Error 400 on SERVER: Failed to parse template test/test.erb: undefined method `+' for nil:NilClass

Krzysztof Wilczynski

unread,
Apr 24, 2012, 10:09:44 AM4/24/12
to puppet...@googlegroups.com
Hi,

Would that help?

 kwilczynski@desktop:~$ irb
irb(main):001:0> require 'erb'
=> true
irb(main):002:0> t = ERB.new <<-EOS
irb(main):003:0" <%= c = c ? c += 1 : 1 %>
irb(main):004:0" EOS
=> #<ERB:0x7f2cde07cbe8 @safe_level=nil, @src="_erbout = ''; _erbout.concat(( c = c ? c += 1 : 1 ).to_s); _erbout.concat \"\\n\"\n; _erbout", @filename=nil>
irb(main):005:0> t.result(binding)
=> "1\n"
irb(main):006:0> t.result(binding)
=> "2\n"
irb(main):007:0> t.result(binding)
=> "3\n"
irb(main):008:0>

KW

AnOnJoe

unread,
Apr 24, 2012, 10:18:56 AM4/24/12
to puppet...@googlegroups.com
I've got it :

Compteur <%= compteur %>
<% compteur = compteur.to_i  -%>
<% compteur += 1 -%>
Compteur <%= compteur %>

Krzysztof Wilczynski

unread,
Apr 24, 2012, 11:41:37 AM4/24/12
to puppet...@googlegroups.com
Hi,

\o/

KW
Reply all
Reply to author
Forward
0 new messages