creationix / haml-js

Haml ported to server-side Javascript. This is a traditional server-side templating language. Tested with node-js
MIT License
901 stars 110 forks source link

Verbose plugin syntax #5

Closed aeosynth closed 14 years ago

aeosynth commented 14 years ago

Hi, I understand that using JSON syntax makes the parser's job easier, but the resulting code looks awkward for humans. If I may be so bold, I'd like to propose the following syntax changes:

Instead of :if{ condition : todolist.length > 20 } use :if{ todolist.length > 20 }

instead of :foreach{ array : todolist, value : "item" } use :foreach{ item in todolist }

creationix commented 14 years ago

I'm currently in a huge refactor in order to fix some outstanding bugs. I'll revise the plugun system to allow for such syntactic suger if I'm able to do it generally. Otherwise I'll just make loops and conditionals language constructs instead of plugins.

tj commented 14 years ago

I was going to patch this up and send it over. The foreach confused me at first, makes sense but it could easily be much cleaner. If I have some time I will try and help, do you have a branch up?

creationix commented 14 years ago

not yet. I usually don't do branches (too many years of subversion I guess) I think I'll make one for the rewrite.

When I wrote the code I didn't know that V8 had all those neat ES5 features. I can write the code a lot more elegant and less buggy.

tj commented 14 years ago

cool ya for sure. its to bad v8 does not support the newer stuff yet :(. definitely suggest a branch for that lol encase you want to port over some bug fixes along the way. Looking forward to the changes, hopefully ill have time to finish my sass crap soon

creationix commented 14 years ago

The new syntax is super simple:

:for key, value in some_object
  Content goes here

:if some_condition
  More content

See the readme for more details.

Fixed in latest release. Enjoy!

tj commented 14 years ago

dope

tj commented 14 years ago

any specific reason why its not similar to regular Haml with

etc

aeosynth commented 14 years ago

Probably because

Here is a list of features not implemented

  • Arbitrary code "-" prefix
creationix commented 14 years ago

It's actually quite easy to now allow arbitrary code, but as you well know, it's not that simple to iterate in pure javascript. Plus you'd have to manually put in your closing braces/parens/anything else you might need to close the loop.

I made the syntax different on purpose since it's really a macro and not direct code.

aeosynth commented 14 years ago

Ruby Blocks

Ruby blocks, like XHTML tags, don’t need to be explicitly closed in Haml. Rather, they’re automatically closed, based on indentation.

In addition to the standard for loop, there's the forEach array method, and the for...in object method (although this is considered harmful). Seems simple enough to me.