Skip to content

Commit

Permalink
fix(eval) further simplify eval statement
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan lawler authored and jordan lawler committed Feb 5, 2020
1 parent fac8b1e commit e71047c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
11 changes: 3 additions & 8 deletions README.md
Expand Up @@ -511,7 +511,7 @@ end

#### template.eval(exp, ctx)

This field is used to intercept expressions pre-execution in order to extend or change behavior. `exp` is a string representation of the primary argument of the expression, for `{{ }}` and `{* *}` delimiters this will be the only argument. `ctx` represents the string representation of additional context for the expression. `{[]}` expressions can take a ctx argument in particular. `template.eval` should return `arg` and optionally `ctx` as strings.
This field is used to intercept expressions pre-execution in order to extend or change behavior. This function is only called for expressions within `{{ }}` and `{* *}` delimiters. `exp` is the string representation of the expression. `template.eval` should return a valid lua expression represented as a string.

```lua
-- setup "safe" function, allowing for expression to evaluate safely
Expand All @@ -525,13 +525,8 @@ template.safe = function(cb)
return res
end

template.eval = function(exp, ctx)
if not ctx then
-- call helper function established above
return "template.safe(function() return " .. exp .. " end)"
end

return exp, ctx
template.eval = function(exp)
return "template.safe(function() return " .. exp .. " end)"
end
```

Expand Down
7 changes: 2 additions & 5 deletions lib/resty/template.lua
Expand Up @@ -181,9 +181,7 @@ do
end
end

function template.eval(...)
return ...
end
function template.eval(exp) return exp end

function template.caching(enable)
if enable ~= nil then caching = enable == true end
Expand Down Expand Up @@ -401,9 +399,8 @@ local ___,blocks,layout={},blocks or {}
if z then
i = s
else
local exp = trim(sub(view, p, e - 1))
c[j] = "___[#___+1]=include("
c[j+1] = eval("[", exp)
c[j+1] = trim(sub(view, p, e - 1))
c[j+2] = ")\n"
j=j+3
s, i = e + 1, e + 2
Expand Down

0 comments on commit e71047c

Please sign in to comment.