GithubHelp home page GithubHelp logo

Comments (9)

pkulchenko avatar pkulchenko commented on June 6, 2024 2

@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.

fonghou avatar fonghou commented on June 6, 2024

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.

pkulchenko avatar pkulchenko commented on June 6, 2024

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.

pkulchenko avatar pkulchenko commented on June 6, 2024

[: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.

pkulchenko avatar pkulchenko commented on June 6, 2024

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.

fonghou avatar fonghou commented on June 6, 2024

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 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.

Let me give it a try. Thank you for answering my questions!

from fullmoon.

pkulchenko avatar pkulchenko commented on June 6, 2024

@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.

fonghou avatar fonghou commented on June 6, 2024

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.

pkulchenko avatar pkulchenko commented on June 6, 2024

Great; thank you for checking! Merged.

from fullmoon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.