lua-users home
lua-l archive

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


At the time when there was a lot of hype for "haml", I created a language for Zena [1] that takes the opposite direction (start from html and uses html tags as variable scoping):

<ul do='people in site'>
  <li do='each' do='link'/>
</ul>

or 
<div do='if' test='visitor.is_anon?'>
  Not logged in
  <r:else>
    Logged in
  </r:else>
</div>

It's like functional PHP with proper variable scoping...

I do not think this is achievable without creating a proper parser + compiler. The extra work is not so big (Use Ragel for parsing for example) but once your template is reduced to an abstract syntax tree, you'll be amazed at how much more stuff you can do.

This is a Ruby thing, but Tir from mongrel2 is pretty close and uses Lua:

{% if #results > 0 then %}
<ul>
    {% for _,result in ipairs(results) do %}
    <li>{{ result }}</li>
    {% end %}
</ul>
{% else %}
<p>We'll add "{{ q }}" as a new one.</p>
{% end %}
Gaspard

[1] http://zenadmin.org

On Thu, May 12, 2011 at 6:40 PM, Romulo <romuloab@gmail.com> wrote:
On Thu, May 12, 2011 at 15:05, Petite Abeille <petite.abeille@gmail.com> wrote:
> On May 12, 2011, at 5:28 PM, Emmanuel Oga wrote:
>> I think writing a web application templates in the way outlined in
>> that example could be a viable option. What do you think?
>
> Well, I personally think it's a baroque waste of time :))

>From my experience, this approach is bad. Your designers won't like
it, your co-workers won't like it. Eventually you won't like it
either. It is easier to just inject Lua into a html file than to write
everything with Lua.

I've used a CherryPy-lke template engine with Orbit's idiom (eg: html{
body{ _if = 'user.logged_in', 'Welcome!' }, body{ _if = 'not
user.logged_in', 'Please login' } } ). However:

1. There was a lot confusion about how to use template's context. A
commom mistake was to try to access directly a context variable: "p{
user.name }" instead of "p{ '${ user.name }' }"
2. Templates got complex very quickly. Too many nested tags, too many
missed colons/curly braces.  No syntax highlighting to aid finding
typos (a proper configured editor could've solved all that, but there
was none).
3. We had to either populate scripts' environment with functions like
html{}, body{}, span{}, ins{}, del{}, table_{}, or to live with with
undetected compile-time typos (i.e.: auto create functions on __index
hit)
4. The most important: every html file had to be translated to Lua's
syntax. Designers didn't quite like me.

Nowadays I use a Django-like template syntax and is working fine so far.

The usage got simpler also:

Template'<html><body>{% if logged_in %}Welcome{% else %}Please login{%
end %}</title><body>...</body>' { logged_in = ctx.session:is_logged()
}

or

Template'/my/proj/templates/simple_site.html'{ logged_in =
ctx.session:is_logged() }

--rb