title: Lua: pass context into loadstring?
body: <p>I'm trying to pass context into a dynamic expression that I evaluate every iteration of a for loop. I understand that the load string only evaluates within a global context meaning local variables are inaccessible. In my case I work around this limitation by converting a local into a global for the purpose of the string evaluation. Here's what I have:</p>
  
  <pre><code>require 'cosmo'
  
  model = { { player = "Cliff", age = 35, gender = "male" }, { player = "Ally", age = 36, gender = "female" }, { player = "Jasmine", age = 13, gender = "female" }, { player = "Lauren", age = 6.5, gender = "female" } }
  
  values = { eval = function(args)
      output = ''
      condition = assert(loadstring('return ' .. args.condition))
      for _, it in ipairs(model) do
          each = it
          if condition() then
              output = output .. each.player .. ' age: ' .. each.age .. ' ' .. '\n'
          end
      end
      return output
  end }
  template = "$eval{ condition = 'each.age &lt; 30' }"
  
  result = cosmo.fill(template, values)
  print (result)
  </code></pre>
  
  <p>My ultimate goal (other than mastering Lua) is to build out an XSLT like tempting engine where I could do something like:</p>
  
  <pre><code>apply_templates{ match = each.age &gt; 30}[[&lt;parent-player&gt;$each.player&lt;/parent-player&gt;]]
  
  apply_templates{ match = each.age &gt; 30}[[&lt;child-player&gt;$each.player&lt;/child-player&gt;]]
  </code></pre>
  
  <p>...And generate different outputs. Currently I'm stuck on my above hawkish means of sharing a local context thru a global. Does anyone here have better insight on how I'd go about doing what I'm attempting to do?</p>