Comments (9)
@fonghou, thank you for the feedback; glad you like it!
I'm wondering if fmg table can be passed directly in lua code as 2nd parameter in setTemplate call.
Yes, you can definitely do that, but you need to add a type field to indicate that you want "fmg"-type processing:
fm.setTemplate(tmpl1, {type = "fmg", [[{
doctype, body{h1{title}, "<!>", raw"<!-- -->"},
div{hx={post="url"}},
{"script", "a<b"}, p"text",
table{style=raw"b<a", tr{td"3", td"4", td{table.concat({1,2}, "")}}},
table{"more"}, p{"1"..notitle}, br,
each{function(v) return p{v} end, {3,2,1}},
{"div", a = "\"1'", p{"text+", include{"tmpl2", {title = "T"}}}},
{"iframe", function() return raw{p{1},p{2},p{3}} end},
}]]})
This particular example is included in test.lua
and shows a mix of div{}
and {"div", ...}
usage.
You can also use the table directly, without wrapping it into a string, but then you'll have to include tags as the first element in each table, like this:
fm.setRoute("/", fm.serveContent("fmg",
{{"h1", "Title"}, {"div", a = 1, {"p", checked = true, "text"}}}))
This should produce the following output: <h1>Title</h1><div a="1"><p checked="checked">text</p></div>
The html tags is simply syntax sugar, so using h1{"something"}
is the same as {"h1", "something"}
, which doesn't require any special "environment" to use these tags.
I'm still a bit on the fence about having two different template mechanisms, but I think each of them has its own strengths, so may decide to keep them both.
Let me know how well it's working for you and what you like or don't like about it. Thanks!
from fullmoon.
I tried to use table directly with fennel. Sadly fennel doesn't support mixed table syntax. [:tag ...] works fine but can't mix naked key/value pairs in the middle. I wish direct table syntax would wrap them in a table object (sample in comment) like clojure hiccup. BTW, I think naked key/value syntax look nice in string format. Hope it won't complicate parser too much to translate between two syntax.
(fm.setRoute "/test"
(fm.serveContent :fmg
[[:doctype]
[:html
[:body [:ol [:li "One"] ;; [:li {:id 1} "One"]
[:li "Two"]]]]]))
from fullmoon.
I'm not familiar enough with fennel to figure out what Lua table this fennel example can correspond to. Can you show how a Lua table may look like to work with this?
It may be difficult to support yet another format, but what you can do is to create a new template format (let's say "fen") and write your own function for it, which would convert it into the structure that matches the one for "fmg" and then does its processing.
To achieve that, you can pass a function to setTemplate
as the second parameter, which is going to be responsible for rendering the template when the render
function is called. This mechanism is used in fullmoon internally and is currently undocumented, so there is a risk that it's going to change.
from fullmoon.
[:tag ...] works fine but can't mix naked key/value pairs in the middle.
BTW, what do you mean by "in the middle"? The order of hash/array values doesn't matter in Lua, so {"div", a = 1, {"p", checked = true, "text"}}
is the same as {"div", {"p", checked = true, "text"}, a = 1}
and is the same as {a = 1, "div", {"p", checked = true, "text"}}
, so maybe one of these ways works for you.
from fullmoon.
I wish direct table syntax would wrap them in a table object (sample in comment) like clojure hiccup.
I did look into that and one of the early prototypes had that indeed, but the table values are used for child elements, so using them for attributes would require that table in a particular position, which would require that table even if the attributes are not provided: {"div", {}, {}}
where the table at the second position would be for attributes and the rest would be for children.
I didn't quite like that, so I decided against that. It may be possible to check if any table has positional values and if now assume that it's a table with attributes, but it makes error checking/reporting a bit more difficult.
from fullmoon.
I'm not familiar enough with fennel to figure out what Lua table this fennel example can correspond to. Can you show how a Lua table may look like to work with this?
Something like below.
fm.setRoute("/", fm.serveContent("fmg",
{{"h1", "Title"},
{"div", { a = 1 },
{"p", {checked = true}, "text"}}}))
translate it to fennel (ie. [] is array only, {} is map only)
(fm.setRoute "/" (fm.serveContent :fmt
[[:h1 "Title"]
[:div {:a 1}
[:p {:checked true} "text]]]
))
It may be difficult to support yet another format, but what you can do is to create a new template format (let's say "fen") and write your own function for it, which would convert it into the structure that matches the one for "fmg" and then does its processing.
Agree that's an option.
To achieve that, you can pass a function to
setTemplate
as the second parameter, which is going to be responsible for rendering the template when therender
function is called. This mechanism is used in fullmoon internally and is currently undocumented, so there is a risk that it's going to change.
Let me give it a try. Thank you for answering my questions!
from fullmoon.
@fonghou, I pushed changes to a separate branch that should allow to do what you were originally trying to do (3747f38). Give it a try and let me know.
from fullmoon.
Hi @pkulchenko, a quick test works perfectly!
(fm.setRoute "/view"
(fm.serveContent :fmg
[[:doctype]
[:html
[:body [:ol [:li {:id 1} "one"]
[:li {:id 2} "two"]]]]]))
outputs <!doctype html><html><body><ol><li id="1">one</li><li id="2">two</li></ol></body></html>
This and current lua table syntax still works, so nice!
Really appreciate you developing this feature!
from fullmoon.
Great; thank you for checking! Merged.
from fullmoon.
Related Issues (20)
- hand over route back to the redbean core if no matching fullmoon route HOT 3
- Uncertain how to obtain query parameters HOT 6
- Any CRUD example for using SQLite? HOT 2
- Any help with DNS/virtual hosting/ip address? HOT 3
- Support for multipart/form-data to allow uploading files HOT 10
- serveRedirect parameter order HOT 1
- [Suggestion] Disable Listing HOT 4
- [Suggestion] Built-in DDOS Protection HOT 3
- Processing layouts HOT 6
- multipart form validation HOT 4
- using booleans in oneof rules HOT 2
- In the instructions: "curl -o redbean.com https://justine.lol/redbean/redbean-latest.com" does not work HOT 3
- makeStorage support for in memory database HOT 4
- Provide DB synchronization HOT 1
- Usage of makeStorage in requests HOT 16
- [Feature Request] To add automatic letsencrypt DNS-01 challenge just like caddy web server HOT 4
- Windows Defender Virus Detected HOT 4
- Custom template engines? HOT 4
- Getting the body of the request HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fullmoon.