Skip to content

Commit 93dcd76

Browse files
committed
Optimize code space; fix docs
1 parent d33f074 commit 93dcd76

File tree

9 files changed

+83
-96
lines changed

9 files changed

+83
-96
lines changed

docs/api.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ aspect:display("dashboard.tpl", {
5454
})
5555
```
5656

57+
## Render result
58+
59+
```lua
60+
local output, err = aspect:display(template, vars)
61+
```
62+
63+
- `output` is `aspect.output` object and contains rendering information, even if the rendering failed.
64+
If you use `aspect:render` method `output` contains rendered string:
65+
```lua
66+
local output, err = aspect:render(template, vars)
67+
if not err then
68+
io.write(tostring(output))
69+
end
70+
```
71+
- `err` is `aspect.error` object and contains error information. `nil` if no errors.
72+
5773
Rendering Templates
5874
-------------------
5975

@@ -442,3 +458,10 @@ end
442458
```twig
443459
{{ data.raw|e("csv") }}
444460
```
461+
462+
## Iterator
463+
464+
The `Aspect` implements custom iterators as in Lua 5.2 - through the metatable and `__pairs()` function.
465+
Works for all lua/luajit versions.
466+
467+
For example see [range iterator](../src/aspect/utils/range.lua).

rockspec/aspect-1.6-1.rockspec

Lines changed: 0 additions & 49 deletions
This file was deleted.

rockspec/aspect-1.6-0.rockspec renamed to rockspec/aspect-1.9-0.rockspec

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package = "aspect"
2-
version = "1.6-0"
2+
version = "1.9-0"
33
source = {
4-
url = "https://github.com/unifire-app/aspect/archive/1.6.zip",
5-
dir = "aspect-1.6"
4+
url = "https://github.com/unifire-app/aspect/archive/1.9.zip",
5+
dir = "aspect-1.9"
66
}
77
description = {
8-
summary = "Aspect is a compiling templating engine for Lua and OpenResty with syntax Twig/Django/Jinja.",
8+
summary = "Aspect is a compiling templating engine for Lua and OpenResty with syntax Twig/Jinja/Liquid.",
99
detailed = [[
10-
* Well known: The most popular Django-like syntax is used - Twig compatible and Jinja like.
10+
* Well known: The most popular Liquid-like syntax is used - Twig compatible and Jinja like.
1111
* Fast: Aspect compiles templates down to plain optimized Lua code.
1212
Moreover, Lua code compiles into bytecode - the fastest representation of a template.
1313
* Secure: Aspect has a sandbox mode to evaluate all template code.
@@ -45,5 +45,7 @@ build = {
4545
["aspect.utils.batch"] = "src/aspect/utils/batch.lua",
4646
["aspect.utils.range"] = "src/aspect/utils/range.lua",
4747
["aspect.loader.array"] = "src/aspect/loader/array.lua",
48+
["aspect.loader.array"] = "src/aspect/loader/filesystem.lua",
49+
["aspect.loader.array"] = "src/aspect/loader/resty.lua",
4850
}
4951
}

rockspec/aspect-git-1.rockspec

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ source = {
66
description = {
77
summary = "Aspect is a compiling templating engine for Lua and OpenResty with syntax Twig/Django/Jinja.",
88
detailed = [[
9-
* Popular Django syntax compatible with Twig2 and Jinja2
10-
* Bytecode and lua code caching of compiled templates are available.
11-
* Safe launch of templates in the sandbox.
12-
* Automatic casting of data types when used.
13-
* Designed for highload and big input and output data.
14-
* Detailed error messages make it easy to debug templates.
15-
* Easily extensible: add your own tags, filters, functions, handlers, operators.
16-
* Intuitive behavior. For example, unlike lua, zero, an empty string, csjon.null, and so on, will be false in if-conditions. All of this is extensible.
17-
* Custom behavior with userdata.
18-
* Supports work with OpenResty.
9+
* Well known: The most popular Liquid-like syntax is used - Twig compatible and Jinja like.
10+
* Fast: Aspect compiles templates down to plain optimized Lua code.
11+
Moreover, Lua code compiles into bytecode - the fastest representation of a template.
12+
* Secure: Aspect has a sandbox mode to evaluate all template code.
13+
This allows Aspect to be used as a template language for applications where users may modify the template design.
14+
* Flexible: Aspect is powered by a flexible lexer and parser.
15+
This allows the developer to define their own custom tags, filters, functions and operators, and to create their own DSL.
16+
* Comfortable: Aspect allows you to process userdata data.
17+
More intuitive behavior with special values such as a empty string, number zero and so on.
18+
* Memory-safe: The template is built in such a way as to save maximum memory when it is executed, even if the iterator provides a lot of data.
1919
]],
2020
license = "BSD-3-Clause",
2121
}
@@ -44,5 +44,7 @@ build = {
4444
["aspect.utils.batch"] = "src/aspect/utils/batch.lua",
4545
["aspect.utils.range"] = "src/aspect/utils/range.lua",
4646
["aspect.loader.array"] = "src/aspect/loader/array.lua",
47+
["aspect.loader.array"] = "src/aspect/loader/filesystem.lua",
48+
["aspect.loader.array"] = "src/aspect/loader/resty.lua",
4749
}
4850
}

src/aspect/compiler.lua

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -895,28 +895,19 @@ end
895895
--- @param name string the tag name
896896
--- @param code_space table|nil for lua code
897897
--- @return aspect.tag
898-
function compiler:push_tag(name, code_space, code_space_name, var_space, push_state)
898+
function compiler:push_tag(name, code_space, code_space_name, var_space)
899899
self.idx = self.idx + 1
900-
if push_state == nil then
901-
push_state = true
902-
end
903900
--- @type aspect.tag
904901
local tag = {
905902
id = self.idx,
906903
name = name,
907-
line = self.line,
908-
push_state = push_state
904+
line = self.line
909905
}
910906
if code_space then
911907
insert(self.code, code_space)
912-
if not code_space_name then
913-
code_space_name = "nil"
914-
else
915-
code_space_name = quote_string(code_space_name)
916-
end
917-
tag.code_space_name = code_space_name
918-
if push_state then
919-
code_space[#code_space + 1] = "__:push_state(_self, " .. self.line .. ", " .. code_space_name .. ")"
908+
if code_space_name then
909+
tag.code_space_name = code_space_name
910+
code_space[#code_space + 1] = "__:push_state(_self, " .. self.line .. ", " .. quote_string(code_space_name) .. ")"
920911
end
921912
self.prev_line = self.line
922913
tag.code_space_no = #self.code
@@ -973,7 +964,7 @@ function compiler:pop_tag(name)
973964
compiler_error(nil, "compiler", "invalid code space layer in the tag " .. name)
974965
else
975966
local prev = remove(self.code)
976-
if tag.push_state then
967+
if tag.code_space_name then
977968
prev[#prev + 1] = "__:pop_state()"
978969
end
979970
end

src/aspect/output.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ function output:set_print(p, size)
160160
self.size = size
161161
end
162162

163+
--- Push new state to call stack
164+
--- @param view aspect.view
165+
--- @param line number
166+
--- @param scope_name string
163167
function output:push_state(view, line, scope_name)
164168
if #self.stack > self.opts.stack_size then
165169
runtime_error(self, "Call stack overflow (maximum " .. self.opts.stack_size .. ")")

src/aspect/tags.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ function tags.tag_for(compiler, tok)
380380
compiler_error(tok, "syntax", "expecting variable name")
381381
end
382382
end
383-
local tag = compiler:push_tag('for', {}, nil, nil, false)
383+
local tag = compiler:push_tag('for', {})
384384
from = compiler:parse_expression(tok:require("in"):next())
385385
tag.has_loop = false
386386
tag.from = from

src/aspect/template.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ local _ = {}
5555
--- @field bytecode_load fun(name: string, tpl: aspect.template):string
5656
--- @field bytecode_save fun(name: string, bytecode: string, tpl: aspect.template)
5757
local template = {
58-
_VERSION = "1.8",
58+
_VERSION = "1.9",
5959
_NAME = "aspect",
6060
}
6161
local mt = { __index = template }

src/aspect/utils/range.lua

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,26 @@ local setmetatable = setmetatable
22
local floor = math.floor
33
local abs = math.abs
44

5-
local function iterator(self, k)
5+
--- Range iterator
6+
--- @class aspect.utils.range
7+
--- @field from number
8+
--- @field to number
9+
--- @field step number
10+
--- @field incr boolean
11+
local range = {}
12+
13+
--- Magic function for {{ for }} tag
14+
--- @return fun iterator (see range.__iterate)
15+
--- @return table context object
16+
--- @return number initial key value
17+
function range:__pairs()
18+
return self.__iterate, self, 0
19+
end
20+
21+
--- Iterator
22+
--- @return number the key
23+
--- @return number the value
24+
function range:__iterate(k)
625
local i = self.from + k * self.step
726
if self.incr then
827
if i > self.to then
@@ -19,30 +38,26 @@ local function iterator(self, k)
1938
end
2039
end
2140

22-
local function __pairs(self)
23-
return iterator, self, 0
24-
end
25-
26-
local function __count(self)
41+
--- Magic function for calculating the number of iterations (elements)
42+
--- @return number count of iterations/elements
43+
function range:__count()
2744
if self.incr then
2845
return floor(abs((self.to - self.from) / self.step)) + 1
2946
else
3047
return floor(abs((self.from - self.to) / self.step)) + 1
3148
end
3249
end
3350

34-
--- @class aspect.utils.range
35-
--- @field from number
36-
--- @field to number
37-
--- @field step number
38-
--- @field incr boolean
39-
local range = {}
4051
local mt = {
4152
__index = range,
42-
__pairs = __pairs,
43-
__count = __count
53+
__pairs = range.__pairs,
54+
__count = range.__count
4455
}
4556

57+
--- Range constructor
58+
--- @param from number
59+
--- @param to number
60+
--- @param step number
4661
function range.new(from, to, step)
4762
return setmetatable({
4863
incr = from < to,
@@ -52,5 +67,4 @@ function range.new(from, to, step)
5267
}, mt)
5368
end
5469

55-
5670
return range

0 commit comments

Comments
 (0)