Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attribute substitution silently fails or crashes inside for loops #15

Open
eggdropsoap opened this issue Sep 5, 2014 · 0 comments
Open

Comments

@eggdropsoap
Copy link

This haml template demonstrates various ways attribute substitution fails inside for loops:

.attributes-work-outside-loops
  %a(href="#{links[1].url}" title="#{links[1].title}")= links[1].title
  %a(href="#{links[2].url}" title="#{links[2].title}")= links[2].title
-# -# uncomment for crash
-# .accessing-tables-in-attributes-throws-error-in-loops
-#   - for i,link in pairs(links) do
-#     %a(href="#{link.url}" title="#{link.title}")= link.title
.simple-attributes-in-loops-dont-error-but-dont-replace-either-but-DO-work-outside-attributes
  - for i,link in pairs(links) do
    %a(href="#{i}" title="#{i}")= i
.this-workaround-for-loops-is-self-defeating
  - for i,link in pairs(links) do
    = "<a href='" .. link.url .. "' title='" .. link.title .. "'>" .. link.title .. "</a>"
.this-workaround-might-clobber-things
  - for i,link in pairs(links) do
    - locals["i"] = i
    - locals["link"] = link
    %a(href="#{link.url}" title="#{link.title}")= link.title

When stored in bug.haml and used in this Lua:

haml = require "haml"

haml_options = {format = "html5"}
engine = haml.new(haml_options)

links = {}
links[1] = {}
links[1].url = "http://example.com"
links[1].title = "Example dot com"
links[2] = {}
links[2].url = "http://example.com"
links[2].title = "Also example dot com"

locals = {links}
template = "bug.haml"
rendered = engine:render_file(template,locals)
print (rendered)

it produces this output:

<div class='attributes-work-outside-loops'>
  <a href='http://example.com' title='Example dot com'>Example dot com</a>
  <a href='http://example.com' title='Also example dot com'>Also example dot com</a>
</div>
<div class='simple-attributes-in-loops-dont-error-but-dont-replace-either-but-DO-work-outside-attributes'>
  <a href='#{i}' title='#{i}'>1</a>
  <a href='#{i}' title='#{i}'>2</a>
</div>
<div class='this-workaround-for-loops-is-self-defeating'>
  <a href='http://example.com' title='Example dot com'>Example dot com</a>
  <a href='http://example.com' title='Also example dot com'>Also example dot com</a>
</div>
<div class='this-workaround-might-clobber-things'>
  <a href='http://example.com' title='Example dot com'>Example dot com</a>
  <a href='http://example.com' title='Also example dot com'>Also example dot com</a>
</div>

When the "crash" loop in the haml is uncommented, Lua 5.1.5 produces this error:

/home/redacted/local/bin/lua: /home/redacted/local/share/lua/5.1/haml/renderer.lua:139: 
Error in bug.haml at line 7 (offset 278): attempt to index global 'link' (a nil value)
stack traceback:
    [C]: in function 'error'
    /home/redacted/local/share/lua/5.1/haml/renderer.lua:139: in function 'render'
    /home/redacted/local/share/lua/5.1/haml.lua:67: in function </home/redacted/local/share/lua/5.1/haml.lua:64>
    (tail call): ?
    ./demo.lua:21: in main chunk
    [C]: ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant