mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
tmpl: fix extra newline added on @if-@else-@end block (#22289)
This commit is contained in:
parent
e974460b45
commit
d5f0db9847
4 changed files with 42 additions and 17 deletions
|
@ -327,37 +327,28 @@ fn vweb_tmpl_${fn_name}() string {
|
|||
source.writeln(tmpl_str_end)
|
||||
pos := line.index('@if') or { continue }
|
||||
source.writeln('if ' + line[pos + 4..] + '{')
|
||||
source.writeln(tmpl_str_start)
|
||||
source.write_string(tmpl_str_start)
|
||||
continue
|
||||
}
|
||||
if line.contains('@end') {
|
||||
// Remove new line byte
|
||||
source.go_back(1)
|
||||
source.writeln(tmpl_str_end)
|
||||
source.writeln('}')
|
||||
source.writeln(tmpl_str_start)
|
||||
source.write_string(tmpl_str_start)
|
||||
continue
|
||||
}
|
||||
if line.contains('@else') {
|
||||
// Remove new line byte
|
||||
source.go_back(1)
|
||||
source.writeln(tmpl_str_end)
|
||||
pos := line.index('@else') or { continue }
|
||||
source.writeln('}' + line[pos + 1..] + '{')
|
||||
// source.writeln(' } else { ')
|
||||
source.writeln(tmpl_str_start)
|
||||
source.write_string(tmpl_str_start)
|
||||
continue
|
||||
}
|
||||
if line.contains('@for') {
|
||||
// Remove an extra unnecessary newline added before in state == .simple
|
||||
// Can break stuff like Markdown
|
||||
if source.len > 1 {
|
||||
source.go_back(1)
|
||||
}
|
||||
source.writeln(tmpl_str_end)
|
||||
pos := line.index('@for') or { continue }
|
||||
source.writeln('for ' + line[pos + 4..] + '{')
|
||||
source.writeln(tmpl_str_start)
|
||||
source.write_string(tmpl_str_start)
|
||||
continue
|
||||
}
|
||||
if state == .simple {
|
||||
|
@ -365,6 +356,7 @@ fn vweb_tmpl_${fn_name}() string {
|
|||
source.writeln(insert_template_code(fn_name, tmpl_str_start, line))
|
||||
continue
|
||||
}
|
||||
// in_write = false
|
||||
// The .simple mode ends here. The rest handles .html/.css/.js state transitions.
|
||||
|
||||
if state != .simple {
|
||||
|
|
13
vlib/v/tests/tmpl/if_cond.txt
Normal file
13
vlib/v/tests/tmpl/if_cond.txt
Normal file
|
@ -0,0 +1,13 @@
|
|||
aaa
|
||||
@if cond
|
||||
bbb
|
||||
@else
|
||||
zzz
|
||||
@end
|
||||
ccc
|
||||
|
||||
aaa
|
||||
@if cond
|
||||
bbb
|
||||
@end
|
||||
ccc
|
24
vlib/v/tests/tmpl_if_cond_test.v
Normal file
24
vlib/v/tests/tmpl_if_cond_test.v
Normal file
|
@ -0,0 +1,24 @@
|
|||
fn test_tmpl_if_cond() {
|
||||
cond := true
|
||||
processed := $tmpl('tmpl/if_cond.txt')
|
||||
assert processed == 'aaa
|
||||
bbb
|
||||
ccc
|
||||
|
||||
aaa
|
||||
bbb
|
||||
ccc
|
||||
'
|
||||
}
|
||||
|
||||
fn test_tmpl_if_else_cond() {
|
||||
cond := false
|
||||
processed := $tmpl('tmpl/if_cond.txt')
|
||||
assert processed == 'aaa
|
||||
zzz
|
||||
ccc
|
||||
|
||||
aaa
|
||||
ccc
|
||||
'
|
||||
}
|
|
@ -46,10 +46,8 @@ numbers: [1, 2, 3]
|
|||
vlang/ui, downloaded 3201 times.
|
||||
vlang/vtl, downloaded 123 times.
|
||||
|
||||
|
||||
this is not ignored
|
||||
|
||||
|
||||
so, it's basically true"
|
||||
|
||||
assert one().trim_space() == expected
|
||||
|
@ -86,10 +84,8 @@ numbers: [1, 2, 3]
|
|||
vlang/ui, downloaded 3201 times.
|
||||
vlang/vtl, downloaded 123 times.
|
||||
|
||||
|
||||
this is not ignored
|
||||
|
||||
|
||||
so, it's basically true"
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue