I am trying to pass a context into a dynamic expression, which I evaluate each iteration of the for loop. I understand that the download line is only evaluated in a global context, which means that local variables are not available. In my case, I exchange this restriction by converting local to global for string evaluation purposes. Here is what I have:
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 < 30' }" result = cosmo.fill(template, values) print (result)
My ultimate goal (besides mastering Lua) is to build XSLT as a tempting engine where I could do something like:
apply_templates{ match = each.age > 30}[[<parent-player>$each.player</parent-player>]] apply_templates{ match = each.age > 30}[[<child-player>$each.player</child-player>]]
... and generate different outputs. I am currently stuck on my above hawkish means of sharing a local context through a global one. Does anyone here better understand how I will do what I'm trying to do?
Cliff
source share