I''m in the process of documenting templates right now, and I figured I should see what happens when you use them with arrays: $ cat ~/bin/test.pp $values = [this, is, an, array, of, values] $content = template("/tmp/templates/testing.erb") file { "/tmp/temtest": content => $content } $ cat /tmp/templates/testing.erb <% values.each do |val| %> I got <%= val %> from you! <% end %> $ puppet -v ~/bin/test.pp notice: //file=/tmp/temtest/content: synced $ cat /tmp/temtest I got this from you! I got is from you! I got an from you! I got array from you! I got of from you! I got values from you! In other words, arrays work just like you would expect. So, you can do iteration in ''content'' attributes using templates. A far cry from iteration in the language, I know, but it''s a decent start, anyway. -- Luke Kanies http://madstop.com | http://reductivelabs.com | 615-594-8199
On Sun, Aug 27, 2006 at 10:41:06PM -0700, Luke Kanies wrote:> $ cat /tmp/templates/testing.erb > <% values.each do |val| %> > I got <%= val %> from you! > <% end %> > > $ cat /tmp/temtest > > I got this from you! > > I got is from you! > > I got an from you! > > I got array from you! > > I got of from you! > > I got values from you!Does it make sense to use something like: ERB.new(...., 0, "<>") so you don''t get the extra new lines? Kostas
Kostas Georgiou wrote:> > Does it make sense to use something like: > ERB.new(...., 0, "<>") > so you don''t get the extra new lines?You can just put everything on one line to skip the blank lines. For some reason, ERB prints an empty line for the code lines; if there''s a line in your template, there''ll be a line in the output. -- If there is anything the nonconformist hates worse than a conformist, it''s another nonconformist who doesn''t conform to the prevailing standard of nonconformity. --Bill Vaughan --------------------------------------------------------------------- Luke Kanies | http://reductivelabs.com | http://madstop.com
On Fri, Sep 01, 2006 at 02:35:07PM -0500, Luke Kanies wrote:> Kostas Georgiou wrote: > > > > Does it make sense to use something like: > > ERB.new(...., 0, "<>") > > so you don''t get the extra new lines? > > You can just put everything on one line to skip the blank lines.True I can do something like: <% principals.each do |val| %><%= val + "\n" %><% end %> But you still get an extra empty line at the end of the list. At the moment I only use code like this for my .k5login file so the extra line doesn''t hurt but I am sure that I''ll find other uses for arrays and templates soon :)> For some reason, ERB prints an empty line for the code lines; if there''s > a line in your template, there''ll be a line in the output.From the ERB manual: If trim_mode is passed a String containing one or more of the following modifiers, ERB will adjust its code generation as listed: % enables Ruby code processing for lines beginning with % <> omit newline for lines starting with <% and ending in %> > omit newline for lines ending in %> If ERB.new(...., 0, "<>") is used in the parser then you can do something like: <% principals.each do |val| %> Foo <%= val %> Bar. <% end %> and for the cases that you only need the value: <% principals.each do |val| %> <%= val %> <% end %> or <% principals.each do |val| %><%= val + "\n" %><% end %> A change like this could break existing templates though :( Kostas Kostas
On Sep 2, 2006, at 1:37 PM, Kostas Georgiou wrote:> On Fri, Sep 01, 2006 at 02:35:07PM -0500, Luke Kanies wrote: > >> Kostas Georgiou wrote: >>> >>> Does it make sense to use something like: >>> ERB.new(...., 0, "<>") >>> so you don''t get the extra new lines? >> >> You can just put everything on one line to skip the blank lines. > > True I can do something like: > <% principals.each do |val| %><%= val + "\n" %><% end %> > > But you still get an extra empty line at the end of the list. > At the moment I only use code like this for my .k5login file > so the extra line doesn''t hurt but I am sure that I''ll find > other uses for arrays and templates soon :) > >> For some reason, ERB prints an empty line for the code lines; if >> there''s >> a line in your template, there''ll be a line in the output. > > From the ERB manual: > > If trim_mode is passed a String containing one or more of the > following > modifiers, ERB will adjust its code generation as listed: > % enables Ruby code processing for lines beginning with % > <> omit newline for lines starting with <% and ending in %> >> omit newline for lines ending in %> > > If ERB.new(...., 0, "<>") is used in the parser then you can do > something > like: > <% principals.each do |val| %> > Foo <%= val %> Bar. > <% end %> > > and for the cases that you only need the value: > <% principals.each do |val| %> > <%= val %> > > <% end %> > or > <% principals.each do |val| %><%= val + "\n" %><% end %> > > A change like this could break existing templates though :(If you send me a patch and a test for this, I''ll commit it. I agree it''s a good idea. -- Luke Kanies http://madstop.com | http://reductivelabs.com | 615-594-8199
On Sat, Sep 02, 2006 at 07:37:23PM +0100, Kostas Georgiou wrote:> On Fri, Sep 01, 2006 at 02:35:07PM -0500, Luke Kanies wrote: > > Kostas Georgiou wrote: > > > > > > Does it make sense to use something like: > > > ERB.new(...., 0, "<>") > > > so you don''t get the extra new lines? > > > > You can just put everything on one line to skip the blank lines. > > True I can do something like: > <% principals.each do |val| %><%= val + "\n" %><% end %> > > But you still get an extra empty line at the end of the list. > At the moment I only use code like this for my .k5login file > so the extra line doesn''t hurt but I am sure that I''ll find > other uses for arrays and templates soon :) > > > For some reason, ERB prints an empty line for the code lines; if there''s > > a line in your template, there''ll be a line in the output. > > From the ERB manual: > > If trim_mode is passed a String containing one or more of the following > modifiers, ERB will adjust its code generation as listed: > % enables Ruby code processing for lines beginning with % > <> omit newline for lines starting with <% and ending in %> > > omit newline for lines ending in %>Is there a modifier for the -%> syntax that Rails uses to signify "eat the EOL"? I quite like that one -- it''s explicit, already widely(ish) known (through Rails), and it''s already bitten me a few times that Puppet''s ERB doesn''t support it. <grin> - Matt -- A byte walks into a bar and orders a pint. Bartender asks him "What''s wrong?" The byte says "Parity error." Bartender nods and says "Yeah, I thought you looked a bit off."
On Sun, Sep 03, 2006 at 07:56:27AM +1000, Matthew Palmer wrote:> On Sat, Sep 02, 2006 at 07:37:23PM +0100, Kostas Georgiou wrote: > > > > From the ERB manual: > > > > If trim_mode is passed a String containing one or more of the following > > modifiers, ERB will adjust its code generation as listed: > > % enables Ruby code processing for lines beginning with % > > <> omit newline for lines starting with <% and ending in %> > > > omit newline for lines ending in %> > > Is there a modifier for the -%> syntax that Rails uses to signify "eat the > EOL"? I quite like that one -- it''s explicit, already widely(ish) known > (through Rails), and it''s already bitten me a few times that Puppet''s ERB > doesn''t support it. <grin>Ah this is much better and it''s not going to break any existing templates. For some reason it''s not documented but setting trim_mode to ''-'' does the trick. Here is a quick patch... --- lib/puppet/parser/scope.rb-old 2006-09-03 00:08:03.000000000 +0100 +++ lib/puppet/parser/scope.rb 2006-09-03 00:06:21.000000000 +0100 @@ -50,7 +50,7 @@ def result result = nil benchmark(:debug, "Interpolated template #{@file}") do - template = ERB.new(File.read(@file)) + template = ERB.new(File.read(@file),0,''-'') result = template.result(binding) end Kostas
On Sun, Sep 03, 2006 at 12:13:50AM +0100, Kostas Georgiou wrote:> On Sun, Sep 03, 2006 at 07:56:27AM +1000, Matthew Palmer wrote: > > Is there a modifier for the -%> syntax that Rails uses to signify "eat the > > EOL"? I quite like that one -- it''s explicit, already widely(ish) known > > (through Rails), and it''s already bitten me a few times that Puppet''s ERB > > doesn''t support it. <grin> > > Ah this is much better and it''s not going to break any existing templates. > For some reason it''s not documented but setting trim_mode to ''-'' does the > trick. > > Here is a quick patch...Stuck in the Trac as http://reductivelabs.com/cgi-bin/puppet.cgi/ticket/258 - Matt
Matthew Palmer wrote:> On Sun, Sep 03, 2006 at 12:13:50AM +0100, Kostas Georgiou wrote: >> On Sun, Sep 03, 2006 at 07:56:27AM +1000, Matthew Palmer wrote: >>> Is there a modifier for the -%> syntax that Rails uses to signify "eat the >>> EOL"? I quite like that one -- it''s explicit, already widely(ish) known >>> (through Rails), and it''s already bitten me a few times that Puppet''s ERB >>> doesn''t support it. <grin> >> Ah this is much better and it''s not going to break any existing templates. >> For some reason it''s not documented but setting trim_mode to ''-'' does the >> trick. >> >> Here is a quick patch... > > Stuck in the Trac as http://reductivelabs.com/cgi-bin/puppet.cgi/ticket/258So this should always be enabled? Is that acceptable, or are people going to want it to just be an option? -- Chase after truth like hell and you''ll free yourself, even though you never touch its coat-tails. -- Clarence Darrow --------------------------------------------------------------------- Luke Kanies | http://reductivelabs.com | http://madstop.com
On Sat, Sep 02, 2006 at 08:12:26PM -0500, Luke Kanies wrote:> Matthew Palmer wrote: > > On Sun, Sep 03, 2006 at 12:13:50AM +0100, Kostas Georgiou wrote: > >> On Sun, Sep 03, 2006 at 07:56:27AM +1000, Matthew Palmer wrote: > >>> Is there a modifier for the -%> syntax that Rails uses to signify "eat the > >>> EOL"? I quite like that one -- it''s explicit, already widely(ish) known > >>> (through Rails), and it''s already bitten me a few times that Puppet''s ERB > >>> doesn''t support it. <grin> > >> Ah this is much better and it''s not going to break any existing templates. > >> For some reason it''s not documented but setting trim_mode to ''-'' does the > >> trick. > >> > >> Here is a quick patch... > > > > Stuck in the Trac as http://reductivelabs.com/cgi-bin/puppet.cgi/ticket/258 > > So this should always be enabled? Is that acceptable, or are people > going to want it to just be an option?I don''t see that it''s ever going to want to be optional -- it''s not as if the syntax can possibly conflict with any other usage, so if you don''t want it, don''t use it. - Matt -- Some people are like slinkies. They are fun and don''t actually serve any real purpose, but they still make you smile when you push them down the stairs.
Matthew Palmer wrote:> > I don''t see that it''s ever going to want to be optional -- it''s not as if > the syntax can possibly conflict with any other usage, so if you don''t want > it, don''t use it.Added in commit 1542. No test, though; if someone wants to commit one, I''d be glad to add it. -- Reality is that which, when you stop believing in it, doesn''t go away. -- Philip K. Dick, "How to Build a Universe" --------------------------------------------------------------------- Luke Kanies | http://reductivelabs.com | http://madstop.com