-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtemplate.d.lua
More file actions
111 lines (109 loc) · 2.54 KB
/
Copy pathtemplate.d.lua
File metadata and controls
111 lines (109 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
---@meta _
---
---String template rendering with <code v-pre>{{...}}</code> placeholders.
---
---## Usage
---
---```lua
---template = mods.template
---
---view = {
--- user = { name = "Ada" },
---}
---
---out = template("Hello {{user.name}}!", view) --> "Hello Ada!"
---```
---
---
---Render string templates with <code v-pre>{{...}}</code> placeholders.
---
---```lua
---view = { subject = "World" }
---template("Hello {{subject}}", view) --> "Hello World"
---```
---
---> [!NOTE]
--->
---> Whitespace inside placeholders is ignored.
--->
---> ```lua
---> template("Hi {{ name }}", { name = "Ada" }) --> "Hi Ada"
---> ```
---
---## Dot Paths
---
---Use dot notation to access nested values in `view`.
---
---```lua
---view = { user = { meta = { role = "Engineer" } } }
---template("Role: {{user.meta.role}}", view) --> "Role: Engineer"
---```
---
---> [!NOTE]
--->
---> <code v-pre>{{.}}</code> renders the entire root `view` table, not a nested field.
--->
---> ```lua
---> template("View: {{.}}", { value = 123 })
---> --> View: {
---> -- value = 123
---> -- }
--->```
---
---## Function Values
---
---If a placeholder resolves to a function, that function is called and its result is rendered.
---
---```lua
---view = { name_func = function() return "Ada" end }
---template("Hi {{name_func}}", view) --> "Hi Ada"
---```
---
---> [!NOTE]
--->
---> If the function returns `nil`, the placeholder renders as an empty string.
---
---
---## Table Values
---
---Table placeholders are rendered using `mods.stringify`.
---
---```lua
---view = { data = { a = 1, b = true } }
---template("Data: {{data}}", view)
-----> Data: {
----- a = 1,
----- b = true
----- }
---```
---
---## Missing and Invalid Placeholders
---
---Missing keys render as an empty string.
---```lua
---view = {}
---template("Missing: {{unknown}}", view) --> "Missing: "
---```
---
---Invalid placeholder names render as an empty string
---(for example: <code v-pre>{{..}}</code>, <code v-pre>{{.name}}</code>,
---<code v-pre>{{user.}}</code>, <code v-pre>{{user..name}}</code>).
---
---```lua
---view = { user = { name = "Ada" } }
---template("Bad: {{user..name}}", view) --> "Bad: "
---```
---
---If a placeholder is not closed (<code v-pre>{{unclosed</code>), it is emitted as-is.
---
---```lua
---view = { name = "Ada" }
---template("Hi {{name", view) --> "Hi {{name"
---```
---
---@param tmpl string Template string with placeholders.
---@param view table Input data used to resolve placeholders.
---@return string out Rendered output string.
---@nodiscard
local function template(tmpl, view) end
return template