mirror of
https://github.com/TangentFoxy/etlua.git
synced 2025-07-27 18:42:17 +00:00
change how values are passed to compiled template, require a buffer pos if passing buffer
This commit is contained in:
@@ -138,8 +138,8 @@ first_fn = parser:load(parser:compile_to_lua("Hello "))
|
|||||||
second_fn = parser:load(parser:compile_to_lua("World"))
|
second_fn = parser:load(parser:compile_to_lua("World"))
|
||||||
|
|
||||||
buffer = {}
|
buffer = {}
|
||||||
parser:run(first_fn, nil, buffer)
|
parser:run(first_fn, nil, buffer, #buffer)
|
||||||
parser:run(second_fn, nil, buffer)
|
parser:run(second_fn, nil, buffer, #buffer)
|
||||||
|
|
||||||
print(table.concat(buffer)) -- print 'Hello World'
|
print(table.concat(buffer)) -- print 'Hello World'
|
||||||
```
|
```
|
||||||
|
10
etlua.moon
10
etlua.moon
@@ -54,7 +54,7 @@ class Compiler
|
|||||||
@push ... if ...
|
@push ... if ...
|
||||||
|
|
||||||
header: =>
|
header: =>
|
||||||
@push "local _b, _b_i, _tostring, _concat, _escape = ...\n"
|
@push "local _tostring, _escape, _b, _b_i = ...\n"
|
||||||
|
|
||||||
footer: =>
|
footer: =>
|
||||||
@push "return _b"
|
@push "return _b"
|
||||||
@@ -220,14 +220,18 @@ class Parser
|
|||||||
fn
|
fn
|
||||||
|
|
||||||
-- takes a function from @load and executes it with correct parameters
|
-- takes a function from @load and executes it with correct parameters
|
||||||
run: (fn, env={}, buffer={}) =>
|
run: (fn, env={}, buffer, i, ...) =>
|
||||||
combined_env = setmetatable {}, __index: (name) =>
|
combined_env = setmetatable {}, __index: (name) =>
|
||||||
val = env[name]
|
val = env[name]
|
||||||
val = _G[name] if val == nil
|
val = _G[name] if val == nil
|
||||||
val
|
val
|
||||||
|
|
||||||
|
unless buffer
|
||||||
|
buffer = {}
|
||||||
|
i = 0
|
||||||
|
|
||||||
setfenv fn, combined_env
|
setfenv fn, combined_env
|
||||||
fn buffer, #buffer, tostring, concat, html_escape
|
fn tostring, html_escape, buffer, i, ...
|
||||||
|
|
||||||
compile_to_lua: (str, ...) =>
|
compile_to_lua: (str, ...) =>
|
||||||
success, err = @parse str
|
success, err = @parse str
|
||||||
|
@@ -109,7 +109,7 @@ This is my message to <%= [=[oh yeah %>"]=] %>]]
|
|||||||
it "should use existing buffer", ->
|
it "should use existing buffer", ->
|
||||||
fn = compile "hello<%= 'yeah' %>"
|
fn = compile "hello<%= 'yeah' %>"
|
||||||
buff = {"first"}
|
buff = {"first"}
|
||||||
out = fn {}, buff
|
out = fn {}, buff, #buff
|
||||||
assert.same "firsthelloyeah", out
|
assert.same "firsthelloyeah", out
|
||||||
|
|
||||||
it "should compile readme example", ->
|
it "should compile readme example", ->
|
||||||
@@ -119,8 +119,8 @@ This is my message to <%= [=[oh yeah %>"]=] %>]]
|
|||||||
second_fn = parser\load parser\compile_to_lua "World"
|
second_fn = parser\load parser\compile_to_lua "World"
|
||||||
|
|
||||||
buffer = {}
|
buffer = {}
|
||||||
parser\run first_fn, nil, buffer
|
parser\run first_fn, nil, buffer, #buffer
|
||||||
parser\run second_fn, nil, buffer
|
parser\run second_fn, nil, buffer, #buffer
|
||||||
|
|
||||||
assert.same "Hello World", table.concat buffer
|
assert.same "Hello World", table.concat buffer
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user