Nex3 wrote:
I agree with everything Seth pointed out. Why not let any amount of
(non-newline) whitespace denote a new block, rather than only two
spaces?
I'd also like to add my voice to those who feel that adding support for
Ruby blocks would be a good idea. If you also added some character, say
'-', that denoted code that was executed but not used as output, then
you could do something like:
- for foo in @foolist
%h1 foo.name
%p foo.text
I'm not against using the - to mean "eval but don't print anything".
However, the problem is the way that its all parsed and executed. We
might be able to work something out, but the deal is that each line is
currently evaluated in-itself.
So this seems more like an implementation issue rather then there is no reason to allow it right? I really like your syntax but I think you will keep getting pushback until multi line iterations and if statements are allowed. I looked at the codebase and I don't think it would be too hard to add a little state to the parse loop to keep track of multi line statements and eval them accordingly.
I'm not sold on the idea that this is a good thing and the
implementation would become much more complex.
However, I still haven't seen any arguements on why this is a Good
Thing, beyond forces of habit, and that some rails helpers use it.
Tradition isn't a good arguement.
What about <% cache do %> blocks? what about content_for :sidebar blocks? there are really a ton of useful situations where the blocks in a view are very important.
Couldn't you do some look ahead when parsing the template? You already are loading all the lines into memory before processing with this line:
template.split(/\n/).map do |line|
What about preprocessing the template to combine multi line statements starting with == into one line statements starting with = and with the compressed lines separated with ;
so this:
%body
#posts
== @posts.each do |post|
== link_to post.title, :action => 'foo', :id => post
== end
becomes this internally after preprocessing before parsing:
%body
#posts
= @posts.each do |post| ; link_to post.title, :action => 'foo', :id => post ; end
Some stupid code that does this preprocessing:
buffer =
seen = false
last = false
template.each_with_index do |line, index|
if line.strip =~ /^==/
unless seen
buffer << line.sub(/==/,'=').chomp
last = index
seen = true
else
buffer[last] << ';'+line.sub('==', '').strip
end
else
seen = false
buffer << line
end
end
And then go into your normal parse loop now that the multi line statements are compressed to single line statements? I am sure there is a better way to do it but this seems feasible.
In the three fairly-complex projects I have done in HAML, besides
breaking habit, I haven't really *needed* blocks. The closest I got was
a pang of rememberance for how i'd do it in rhtml.
Maybe you haven't needed block because you weren't allowed to do them
I think HAML is truly something very cool, I just think it won't get as much adoption as it should until you allow multi line statements period.
just my two cents here of course...
Cheers-
-Ezra