nolate icon indicating copy to clipboard operation
nolate copied to clipboard

Interpolating a sub-template

Open rjp opened this issue 14 years ago • 3 comments

test.rb require './nolate' $foo = [["fish",29],["chips",43],["monkey",57]] print nlt("index.nlt")

views/index.nlt This is a list of items <%= $foo.each do |k| nlt("row.nlt", {:k => k}); end %> This is after the list

views/row.nlt * Item: <%#k[0]%> costs <%#k[1]%>

This doesn't output what I'd expect. What am I doing wrong?

output This is a list of items fish29chips43monkey57 This is after the list

Additionally, if I use i as the variable in the each, it crashes with ./nolate.rb:38:in nolate': undefined method<' for ["monkey", 57]:Array (NoMethodError)

rjp avatar Apr 02 '11 15:04 rjp

Interesting, I think the 'i variable' problem is that I eval without creating a new "clean" context. I should definitely do that otherwise the whole system is unusable.

For the first problem, you need to accumulate the things you generate in the sub-template in a var, and finally end the code with the name of the var.

Thanks for reporting.

antirez avatar Apr 02 '11 15:04 antirez

Aha. <%= a="";$foo.each do |k| a<<=nlt("row.nlt", {:k => k}); end; a %>

Now outputs This is a list of items * Item: costs * Item: costs * Item: costs

This is after the list

Changing row.nlt to be * Item: <%=k[0]%> costs <%=k[1]%>

gives ./nolate.rb:59:in nolate': undefined local variable or methodk' for main:Object (NameError)

Which I guess means I need to put anything I might want out of k directly into the hash passed to row.nlt? Something like this? <%= a="";$foo.each do |k| a<<=nlt("row.nlt", {:k0 => k[0], :k1 => k[1]}); end; a %>

That works.

rjp avatar Apr 02 '11 15:04 rjp

On Sat, Apr 2, 2011 at 5:39 PM, rjp [email protected] wrote:

Changing row.nlt to be    * Item: <%=k[0]%> costs <%=k[1]%>

The trick is using @k in the template, and setting it outside, but I'm looking for better approaches indeed!

Cheers, Salvatore

Salvatore 'antirez' Sanfilippo open source developer - VMware

http://invece.org "We are what we repeatedly do. Excellence, therefore, is not an act, but a habit." -- Aristotele

antirez avatar Apr 02 '11 16:04 antirez