sipin/gorazor

Should it preserve newline and whitespace?

sunfmin opened this issue · 11 comments

hello_gohtml__gorazortests

But it generates:

<ul><li> Felix Sun 0 </li><li> Felix Sun 1 </li><li> Felix Sun 2 </li><li> Felix Sun 3 </li><li> Felix Sun 4 </li><li> Felix Sun 5 </li><li> Felix Sun 6 </li><li> Felix Sun 7 </li><li> Felix Sun 8 </li><li> Felix Sun 9 </li><li> Felix Sun 10 </li><li> Felix Sun 11 </li><li> Felix Sun 12 </li><li> Felix Sun 13 </li><li> Felix Sun 14 </li><li> Felix Sun 15 </li><li> Felix Sun 16 </li><li> Felix Sun 17 </li><li> Felix Sun 18 </li><li> Felix Sun 19 </li><li> Felix Sun 20 </li><li> Felix Sun 21 </li><li> Felix Sun 22 </li><li> Felix Sun 23 </li><li> Felix Sun 24 </li><li> Felix Sun 25 </li><li> Felix Sun 26 </li><li> Felix Sun 27 </li><li> Felix Sun 28 </li><li> Felix Sun 29 </li>
</ul>

newline & space at the end of line will be ignored automatically, because I couldn't think of any reason to preserved them.

This behaviour is enforced in af313d2

I will put document it in readme later.

Or, could you provide an example that such newline & space could be useful? Then, maybe gorazor could make this into a config option.

For example, If I want to generate a markdown list?

@for i := 0; i < count; i++ {
- @fmt.Sprintf("%s %d", name, i)
}

(this code doesn't work)

I want:

- Felix Sun 0
- Felix Sun 1
...

Is razor a general purpose template language? Or it's only for html?

this worked:

@for i := 0; i < count; i++ {
@raw("-") @fmt.Sprintf("%s %d", name, i)
}

But it gives me:

-Felix Sun 0-Felix Sun 1-Felix Sun 2-Felix Sun 3-Felix Sun 4-Felix Sun 5-Felix Sun 6-Felix Sun 7-Felix Sun 8-Felix Sun 9-Felix Sun 10-Felix Sun 11-Felix Sun 12-Felix Sun 13-Felix Sun 14-Felix Sun 15-Felix Sun 16-Felix Sun 17-Felix Sun 18-Felix Sun 19-Felix Sun 20-Felix Sun 21-Felix Sun 22-Felix Sun 23-Felix Sun 24-Felix Sun 25-Felix Sun 26-Felix Sun 27-Felix Sun 28-Felix Sun 29

Which won't be valid markdown list.

Consider:
@for i := 0; i < count; i++ {
@raw("-") @fmt.Sprintf("%s %d\n", name, i)
}

?

Razor is must suitable as html template, as HTML markup could have clear distinction from code.

I once tried to use razor for code generation, and it's one of the worst idea I ever had. ^_^

ok, if I want to add new line or space, I have to do this, that's fine. :-)

@for i := 0; i < count; i++ {
@raw("- ") @name @raw("\n") @raw("     ")
}

But all other template language preserve newline and space, Is the .Net Razor removes them too?

I think the razor command line should remove new line and space. But the text line should keep?

sipin_gorazor

for example:

<div class="@className"></div>
@if true {
    <span> Hello </span>
}

should generate:

<div class="@className"></div>
    <span> Hello </span>

NOT

<div class="@className"></div><span> Hello </span>

just my opinion.

Honestly, I've no idea. Haven't been doing .net development for years.

I'm in favour of this behaviour because of rails:
"rails在很多年前就在模板中内置了一个“-”函数,类似:
<%for 1 to 10 -%>

<%next%>"

You may want to check the discussion at: http://blog.zhaojie.me/2009/12/is-it-really-necessary-to-strip-white-space-in-html.html

PS: I'm 问天, and gorazor could handle

 correctly:

<pre>
fdafdas
fdsafasfd
fdsafasfd

1213 2   2    
fdsafasfd
</pre> 

=>
_buffer.WriteString("<pre>\nfdafdas\nfdsafasfd\nfdsafasfd\n\n1213 2   2    \nfdsafasfd\n</pre> ")

Rails is using erubis, and I think it's keep space

<% 10.times do |i| -%>
   Felix <%= i %>
<% end -%>

output:

felixmbpr:~ sunfmin$ erubis h.erb 
   Felix 0
   Felix 1
   Felix 2
   Felix 3
   Felix 4
   Felix 5
   Felix 6
   Felix 7
   Felix 8
   Felix 9

I think trim all the spaces and newline between two template token is bit hardcore.

the erubis will trim the new line at the token, for example:

<% 10.times do |i| -%>
   Felix <%= i -%>
<% end -%>

Will generate:

felixmbpr:~ sunfmin$ erubis h.erb 
   Felix 0   Felix 1   Felix 2   Felix 3   Felix 4   Felix 5   Felix 6   Felix 7   Felix 8   Felix 9