lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Last year, I was inspired to write a Lua version of the Ruby XML Builder library:
http://builder.rubyforge.org/

I only made as much of it as I needed, and never published it until now:
http://pastie.org/1893954   -- the xml_builder.lua "lib"
http://pastie.org/1893976   -- the test program

Example:
print(
    xm.html(                         -- <html>
        xm.head(                     --   <head>
            xm.title("History")      --     <title>History</title>
        ),                           --   </head>
        xm.body( { class = 'foo' },  --   <body class = "foo">
          xm.__comment "HI",         --     <!-- HI -->
          xm.h1("Header"),           --     <h1>Header</h1>
          xm.div{width="100%"},      --     <div width="100%"/>
          xm.p("paragraph")          --     <p>paragraph</p>
        )                            --   </body>
    )                                -- </html>
)
yields...
<html><head><title>History</title></head><body class="foo"><!-- HI --><h1>Header</h1><div width="100%"/><p>paragraph</p></body></html>


As with Petite Abeille's post, it's amazing how expressive of a DSL you can achieve with a few lines of Lua code.

-Evan


On 5/12/11 8:28 AM, Emmanuel Oga wrote:
Hello, I was looking at this ruby library:

http://hammer.pitr.ch/2011/05/11/HammerBuilder-introduction/

In which you can do things like:

HammerBuilder::Formated.get.go_in do
   xhtml5!
   html do
     head { title 'my_page' }
     body do
       div :id =>  'content' do
         p "my page's content", :class =>  'centered'
       end
     end
   end
end.to_xhtml! # =>

I started to think this could be written more succinctly in lua:

local t = {
   xhtml5,
   html {
     head { title {'my_page'} },
     body { id='content',
       p { class='centered', "my page's content"}
     }
   }
}

It does not look that bad, does it? Here is some quick and dirty code
to render that template:

http://pastie.org/1893384

I'm wondering if there is already a lua project that handles a
template like in the example above.

I think writing a web application templates in the way outlined in
that example could be a viable option. What do you think?








--------------------------------------------------------------
EmmanuelOga.com - Software Developer