diff --git a/src/lustache/renderer.lua b/src/lustache/renderer.lua index b190d52..6d0c39b 100755 --- a/src/lustache/renderer.lua +++ b/src/lustache/renderer.lua @@ -12,7 +12,7 @@ local patterns = { nonSpace = "%S", eq = "%s*=", curly = "%s*}", - tag = "[#\\^/>{&=!]" + tag = "[#\\^/>{&=!?]" } local html_escape_characters = { @@ -27,6 +27,7 @@ local html_escape_characters = { local block_tags = { ["#"] = true, ["^"] = true, + ["?"] = true, } local function is_array(array) @@ -63,6 +64,9 @@ local function compile_tokens(tokens, originalTemplate) for i, token in ipairs(tokens) do local t = token.type buf[#buf+1] = + t == "?" and rnd:_conditional( + token, ctx, subrender(i, token.tokens) + ) or t == "#" and rnd:_section( token, ctx, subrender(i, token.tokens), originalTemplate ) or @@ -208,6 +212,16 @@ function renderer:render(template, view, partials) return fn(view) end +function renderer:_conditional(token, context, callback) + local value = context:lookup(token.value) + + if value then + return callback(context, self) + end + + return "" +end + function renderer:_section(token, context, callback, originalTemplate) local value = context:lookup(token.value)