olivine-labs / lustache Goto Github PK
View Code? Open in Web Editor NEWMustache templates for Lua
Home Page: http://olivinelabs.com/lustache
License: MIT License
Mustache templates for Lua
Home Page: http://olivinelabs.com/lustache
License: MIT License
require("lustache").render("\r")
/usr/local/share/lua/5.1/lustache.lua:333: attempt to call a nil value
Hi,
I tried use Lustache to pre-process Lua script in order to do minifying.
I thought I have to change the 'tags', with a intuitive choice like '{{=--[[< >]]=}}'.
But it cause error when parsing.
It seems it caused by invalid escaping the 'tags'.
When I change escape_tags function in renderer module to:
local function escapeRE(s)return (s:gsub("([^%w])", "%%%1"))end
local function escape_tags(tags)
return {escapeRE(tags[1])..'%s*','%s*'..escapeRE(tags[2]) }
end
, the parsing not causing error anymore, and correctly parse the script now.
Thank you.
The link in the repository description points to:
but since the website support https, it's worth updating it:
For example, to generate
hi chris!
hi mark!
hi scott!
I could write (with a yaml header and lustache)...
---
names: [ {name: chris}, {name: mark}, {name: scott} ]
---
# Hello Everyone
{{#names}}
hi {{name}}!
{{/names}}
The following code gives me stack overflow. Any idea why? Thanks.
local lustache = require'lustache'
local tree = [[
{{#files}}
{{path}}
{{>tree}}
{{/files}}
]]
local root = {
path = '',
files = {
path = 'files',
files = false,
}
}
print(lustache:render(tree, root, {tree = tree}))
Is there a way to get the index of the current element like so:
{{#list}}
{{index}} - {{.}}
{{/list}}
JSON does not allow trailing commas after the last element of a list. Thus the following is legal:
{
"a": "1",
"b": "2"
}
While this is not:
{
"a": "1",
"b": "2",
}
Does lustache support this somehow? Would it be possible to write a function that adds a comma to it's output for all elements of a list but the last?
This simple sample:
local lustache = require "lustache"
local data = {test = "Hello world"}
local tpl = "{{#test}}Test is set to: {{test}}{{/test}}{{^test}}Test is empty{{/test}}"
print(lustache:render(tpl, data))
produces error:
.\lustache\renderer.lua:29: bad argument #1 to 'pairs' (table expected, got string)
This is caused by error in function renderer:_inverted
, function is_array
is called here without test whether the value is table. Test for table value could be added to is_array
function to fix this issue:
local function is_array(array)
if type(array) ~= "table" then return false end
local max, n = 0, 0
for k, _ in pairs(array) do
if not (type(k) == "number" and k > 0 and math_floor(k) == k) then
return false
end
max = math_max(max, k)
n = n + 1
end
return n == max
end
When installing perimeterx-nginx-plugin
the process fails while fetching lustache
as it's dependency.
❯ luarocks install perimeterx-nginx-plugin
...
perimeterx-nginx-plugin 7.1.3-1 depends on lustache (not installed)
Installing https://luarocks.org/lustache-1.3.1-0.rockspec
Error: Failed installing dependency: https://luarocks.org/lustache-1.3.1-0.rockspec - Error fetching file: Failed downloading https://github.com/Olivine-Labs/lustache/archive/v1.3.1-0.tar.gz - v1.3.1-0.tar.gz
❯ curl -v https://github.com/Olivine-Labs/lustache/archive/v1.3.1-0.tar.gz
...
HTTP/2 302
< server: GitHub.com
< date: Thu, 08 Sep 2022 09:12:12 GMT
< content-type: text/html; charset=utf-8
< vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, Accept-Encoding, Accept, X-Requested-With
< permissions-policy: interest-cohort=()
< location: https://codeload.github.com/Olivine-Labs/lustache/tar.gz/refs/tags/v1.3.1-0
A clue might be the 302 redirect. Perhaps the package url needs to be changed
package = "lustache"
version = "1.3.1-0"
source = {
url = "https://github.com/Olivine-Labs/lustache/archive/v1.3.1-0.tar.gz",
dir = "lustache-1.3.1-0"
}
to url = "https://codeload.github.com/Olivine-Labs/lustache/tar.gz/refs/tags/v1.3.1-0",
?
like so ( renderer.lua ):
`local escape_tags_repl = {
["%"] = "%%",
["^"] = "%^",
["$"] = "%$",
["("] = "%(",
[")"] = "%)",
["."] = "%.",
["["] = "%[",
["]"] = "%]",
[""] = "%",
["+"] = "%+",
["-"] = "%-",
["?"] = "%?"
}
local function escape_tags(tags)
--[[ instead of
local result = {
string_gsub(tags[1], "%%", "%%%%").."%s*",
"%s*"..string_gsub(tags[2], "%%", "%%%%"),
}
]]
local delim1 = string_gsub( tags[1], "[%%%^%$%(%)%.%[%]%%+%-%?]", escape_tags_repl ).."%s"
local delim2 = "%s*"..string_gsub( tags[2], "[%%%^%$%(%)%.%[%]%*%+%-%?]", escape_tags_repl )
return { delim1, delim2 }
end
`
lua handler aborted:
runtime error:
/usr/local/share/lua/5.1/lustache.lua:201: attempt to call upvalue 'fn' (a nil value)
I had to manually set the partial_cache from an external persistant cache, because lustache wasn't persisting its cache:
I propose that either lustache be smarter about keeping its cache, or allow passing partials into the function returned by lustache:compile
.
Tested on Windows 10 with lustache 1.3.1-0 and Lua 5.1.5.
While trying to render some of the examples from the readme I got the following error:
lua: .\lustache.lua:404: attempt to call method 'lookup' (a nil value)
stack traceback:
.\lustache.lua:404: in function '_name'
.\lustache.lua:202: in function <.\lustache.lua:188>
(tail call): ?
.\lustache.lua:347: in function '_section'
.\lustache.lua:194: in function <.\lustache.lua:188>
(tail call): ?
(tail call): ?
test-lustache-sections.lua:18: in main chunk
[C]: ?
The test file was
local lustache = require "lustache"
local view_model, template
view_model = {
stooges = {
{ name = "Moe" },
{ name = "Larry" },
{ name = "Curly" },
}
}
template = [[
{{#stooges}}
* {{name}}
{{/stooges}}
]]
print(lustache:render(template, view_model))
The first bug I found was in the function Context:lookup(name)
, where the call to string_find
interprets "." as a pattern (search any character). The line
if string_find(name, ".") > 0 then
should be changed to
if string_find(name, ".", 1, true) then
The second bug is when the function Context:new(view, parent)
sets the wrong metatable when called from Renderer:_section(token, context, callback, originalTemplate)
. My workaround was to change the two callback(context:push(v), self)
calls into callback(Context:push(v), self)
.
solved
my isp is a little... ancient. no luarocks or brew
so how to use this code without luarocks/brew install?
i downloaded the souce. i wrote a file src/dem1.lua
lustache = require "lustache"
view_model = {
title = "Joe",
calc = function ()
return 2 + 4;
end
}
output = lustache.render("{{title}} spends {{calc}}", view)
then i ran it an got an error:
> lua demo1.lua
lua: ./lustache/renderer.lua:183: Call mustache:render, not mustache.render!
stack traceback:
[C]: in function 'error'
./lustache/renderer.lua:183: in function 'render'
demo1.lua:10: in main chunk
[C]: in ?
can u help?
hi all
is possible make a lustache_min.lua with all embed code ?
I do not wish to have HTML escape sequences in my output. To achieve my goal, I commented out the following lines in the file "renderer.lua":
if escape then
return string_gsub(str, '[&<>"\'/]', function(s) return html_escape_characters[s] end)
end
If you have a more elegant way to achieve my goal, let me know.
It would be nice if you would consider adding proper source release tarballs,
as GitHub auto-generated tarballs are not immutable. This is an issue [1] for
operating systems creating packages for lustache. Thanks in advance.
[1] https://marc.info/?l=openbsd-ports&m=151973450514279&w=2
The inverted section handling code implicitly assumes the value is a list, so it breaks when the value is e.g. a string:
model = {
person = {
{ name = "steve", }, -- i.e. job = nil
{ name = "john", job="carpenter" },
},
}
{{#person}}
{{#job}} {{name}} is a {{job}} {{/job}}
{{^job}} {{name}} has no job {{/job}}
{{/person}}
/lustache/renderer.lua:29: bad argument #1 to 'pairs' (table expected, got string)
stack traceback:
[C]: in function 'pairs'
./modules/lustache/renderer.lua:29: in function 'is_array'
./modules/lustache/renderer.lua:242: in function '_inverted'
./modules/lustache/renderer.lua:63: in function 'callback'
./modules/lustache/renderer.lua:211: in function '_section'
./modules/lustache/renderer.lua:60: in function 'render'
The problem is that is_array() assumes the input is a table; this needs to be verified before calling it. Modifying line 242 as follows fixes the issue:
- if value == nil or value == false or (is_array(value) and #value == 0) then
+ if value == nil or value == false or (type(value) == "table" and is_array(value) and #value == 0) then
Hi there,
I'm a Lua newbie and I think I'm doing something wrong.
Check my code (from lustache's README):
lustache = require "lustache"
view_model = {
stooges = {
{ name = "Moe" },
{ name = "Larry" },
{ name = "Curly" }
}
}
template = [[
{{#stooges}}
<b>{{name}}</b>
{{/stooges}}]]
output = lustache:render(template, view_model)
print(output)
Output:
<b>Moe</b>
<b>Larry</b>
<b>Curly</b>
My first guess was related to mustache, so I tried it in http://trymustache.com and the output was the following.
Output:
<b>Moe</b>
<b>Larry</b>
<b>Curly</b>
After, I tried doing the same using Ruby, because I know it better. Check it out:
require 'mustache'
view_model = {
:stooges => [
{ :name => "Moe" },
{ :name => "Larry" },
{ :name => "Curly" }
]
}
template = <<-EOS
{{#stooges}}
<b>{{name}}</b>
{{/stooges}}
EOS
output = Mustache.render(template, view_model)
puts output
Output:
<b>Moe</b>
<b>Larry</b>
<b>Curly</b>
Am I doing something wrong with lustache?
Mustache has an extensive spec which doubles as a test-suite. Each test comes with sample data, a template and the expected output, all formatted in standard YAML (even the strange {key: value}
syntax is standard YAML). The procedure for conformance testing is also described in the specs. Thus this should be a matter of loading the YAML files, iterating over all tests, setting up context and partials appropriately, pushing the template through the render
function and comparing the result with the sample output.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.