diff --git a/vlib/v/parser/tmpl.v b/vlib/v/parser/tmpl.v index ee55801b5c9ef7..b3fe8682f2405e 100644 --- a/vlib/v/parser/tmpl.v +++ b/vlib/v/parser/tmpl.v @@ -242,6 +242,7 @@ fn vweb_tmpl_${fn_name}() string { mut end_of_line_pos := 0 mut start_of_line_pos := 0 mut tline_number := -1 // keep the original line numbers, even after insert/delete ops on lines; `i` changes + mut in_write := false // continuing string write code for i := 0; i < lines.len; i++ { line := lines[i] tline_number++ @@ -327,44 +328,43 @@ 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) + in_write = true 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) 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) + in_write = true 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 { // by default, just copy 1:1 - source.writeln(insert_template_code(fn_name, tmpl_str_start, line)) + if in_write { + source.write_string(insert_template_code(fn_name, tmpl_str_start, line)) + } else { + source.writeln(insert_template_code(fn_name, tmpl_str_start, line)) + } + in_write = false continue } + in_write = false // The .simple mode ends here. The rest handles .html/.css/.js state transitions. if state != .simple { diff --git a/vlib/v/tests/tmpl_test.v b/vlib/v/tests/tmpl_test.v index a80f6cdecd4b9b..aeecaa309c0e13 100644 --- a/vlib/v/tests/tmpl_test.v +++ b/vlib/v/tests/tmpl_test.v @@ -32,6 +32,7 @@ numbers: [1, 2, 3] 2 3 + 0 - 0 2 - 1 4 - 2 @@ -43,13 +44,13 @@ numbers: [1, 2, 3] 16 - 8 18 - 9 + vlang/ui, downloaded 3201 times. vlang/vtl, downloaded 123 times. this is not ignored - so, it's basically true" assert one().trim_space() == expected @@ -72,6 +73,7 @@ numbers: [1, 2, 3] 2 3 + 0 - 0 2 - 1 4 - 2 @@ -83,13 +85,13 @@ numbers: [1, 2, 3] 16 - 8 18 - 9 + vlang/ui, downloaded 3201 times. vlang/vtl, downloaded 123 times. this is not ignored - so, it's basically true" } @@ -144,3 +146,16 @@ fn test_tmpl_include_grandchild() { child_tmpl := $tmpl('tmpl/nested/nested_deeper/grandchild.html') assert child_tmpl.contains(expected) } + +fn test_tmpl_if_cond() { + cond := true + processed := $tmpl('tmpl/if_cond.txt') + assert processed == 'aaa +bbb +ccc + +aaa +bbb +ccc +' +}