Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Interpolating a sub-template #2

Open
rjp opened this Issue Apr 2, 2011 · 3 comments

Comments

Projects
None yet
2 participants

rjp commented Apr 2, 2011

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)

Owner

antirez commented Apr 2, 2011

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.

rjp commented Apr 2, 2011

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.

Owner

antirez commented Apr 2, 2011

On Sat, Apr 2, 2011 at 5:39 PM, rjp
reply@reply.github.com
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment