valenok/mongoose — pull request #128 by abadc0de (2013-03-13 18:51:07+00:00)
Title: Better output buffering for Lua pages

This branch adds two Lua functions for output buffering named `ob.push` and `ob.pop` (names negotiable, of course). Output buffers are stackable (nestable) up to a fixed depth, currently 16 levels deep.

`ob.push` creates a new output buffer, increases the depth by one, and returns the new depth, or `nil` if it failed.

`ob.pop` decreases the depth by one and returns the top buffer, or `nil` if it failed.

This should perform better than my previous attempt (https://github.com/valenok/mongoose/pull/123), since we now store data using `luaL_Buffer` and only retrieve it when needed instead of doing string concatenation each time `lsp_write` is called.

Please let me know your thoughts on this; if it needs tweaking I can fix it up. I tried to keep it short, but I can add more safety checks and more functions to deal with buffers (get, set, and depth functions?) if anyone wants those. I personally just need "push" and "pop," and I'm guessing this will cover most normal use cases.

Giving Lua scripts the ability to delay and inspect output has a few advantages, for example:
- Allows "advanced" templating, where templates have arbitrary placeholders for content and can piggyback on one another.
- Allows output to be suspended in cases where headers may still need to be sent.
- Allows rewriting, for example some relative links in an HTML document could be rewritten to point elsewhere under certain conditions (static content server, form action handler on included page fragment, etc.).