https://github.com/Be1zebub/lua-template
php style html+lua inline templater for luvit
https://github.com/Be1zebub/lua-template
html lua luajit luvit template-engine
Last synced: 3 months ago
JSON representation
php style html+lua inline templater for luvit
- Host: GitHub
- URL: https://github.com/Be1zebub/lua-template
- Owner: Be1zebub
- License: gpl-3.0
- Created: 2023-02-24T13:09:57.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-10-07T03:43:41.000Z (over 1 year ago)
- Last Synced: 2024-07-29T19:13:20.293Z (10 months ago)
- Topics: html, lua, luajit, luvit, template-engine
- Language: Lua
- Homepage: https://incredible-gmod.ru
- Size: 54.7 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lua-template
php style html+lua inline templater for luvit
deps: jit `string.buffer`, luvit `uv` (used in `:weblit` for fs watch), luvit `coro-fs` (used in `:include` & `:weblit`) - all expect `string.buffer` is optional# usage
```lua
-- /init.lua
local app = require("weblit-app")app.bind({
host = "127.0.0.1",
port = 80
})app.route({
method = "GET",
path = "/"
}, require("lua-template"):weblit("views/index.html", {app = app}))app.start()
```
```htmlfunction Hex(r, g, b)
return string.format("#%x", (r * 0x10000) + (g * 0x100) + b):upper()
end
?>
h1 {
color: ${ Hex(22, 160, 133) }; <!-- echo codeblock -->
}.user {
display: table;
margin-top: 16px;
}.avatar {
width: 96px;
height: 96px;
border-radius: 50%;
}.user > span {
display: table-cell;
vertical-align: middle;
white-space: pre;
}.name {
font-size: 24px;
}.admin {
color: #c0392b;
}
Current time: ${os.time()}
![]()
${user.name}
(admin)
```

# notes
string buffer support required!
to got string buffer support, you can get latest luvit build here: https://github.com/truemedian/luvit-bin# todo
add templ style feature, i think it looks like a good api
https://templ.guide/syntax-and-usage/elements/```lua
-- обёртка над lua-template, позволяющая упаковывать конструкторы и их env в удобную функцию templ.rendertempl.new("button")
:add("text", tostring) -- имя аргумента, конструктор аргумента
:html("${text}")-- можно указать html код в виде строки
:path("path/to/your/document.html") -- либо в виде пути к файлуtempl.render("button", {text = "Click me"}) -- класс шаблона, аргументы шаблона - внутренне использует luaTemplate:eval(code, env) для рендера
-- пример использования:
templ.render("background")
templ.render("header", {
user = oauth2:GetUser(request.cookie.token)
})
templ.render("promotion", {
search_placeholders = db:GetRandomAddons(5),
featured = db:GetFeatured()
})
templ.render("marketplace", {
newcommers = db:GetLatestAddons(4),
popular = db:GetPopularAddons(12)
})
templ.render("footer", {
stats = {
addonsLen = db:GetAddonsForSale(),
online = db:GetOnlineCount(),
visitors = db:GetUniqueVisitors()
}
})response.body = templ.pop()
```