diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 99d8a7680b..1cc5860c19 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -4829,6 +4829,12 @@ fn (mut g Gen) gen_optional_error(target_type ast.Type, expr ast.Expr) { fn (mut g Gen) return_stmt(node ast.Return) { g.write_v_source_line_info(node.pos) + + g.inside_return = true + defer { + g.inside_return = false + } + if node.exprs.len > 0 { // skip `return $vweb.html()` if node.exprs[0] is ast.ComptimeCall { @@ -4838,10 +4844,6 @@ fn (mut g Gen) return_stmt(node ast.Return) { } } - g.inside_return = true - defer { - g.inside_return = false - } // got to do a correct check for multireturn sym := g.table.get_type_symbol(g.fn_decl.return_type) fn_return_is_multi := sym.kind == .multi_return diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 9689a8e8d8..8fdfa9337e 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -42,6 +42,12 @@ fn (mut g Gen) comptime_call(node ast.ComptimeCall) { } if node.is_vweb { is_html := node.method_name == 'html' + mut cur_line := '' + + if !is_html { + cur_line = g.go_before_stmt(0) + } + for stmt in node.vweb_tmpl.stmts { if stmt is ast.FnDecl { // insert stmts from vweb_tmpl fn @@ -49,19 +55,24 @@ fn (mut g Gen) comptime_call(node ast.ComptimeCall) { if is_html { g.inside_vweb_tmpl = true } - g.stmts(stmt.stmts) + g.stmts(stmt.stmts.filter(it !is ast.Return)) g.inside_vweb_tmpl = false break } } } + if is_html { // return vweb html template g.writeln('vweb__Context_html(&app->Context, _tmpl_res_$g.fn_decl.name); strings__Builder_free(&sb); string_free(&_tmpl_res_$g.fn_decl.name);') } else { // return $tmpl string fn_name := g.fn_decl.name.replace('.', '__') - g.writeln('return _tmpl_res_$fn_name;') + g.write(cur_line) + if g.inside_return { + g.write('return ') + } + g.write('_tmpl_res_$fn_name') } return } diff --git a/vlib/v/parser/tmpl.v b/vlib/v/parser/tmpl.v index cecaeac3b0..796da60e42 100644 --- a/vlib/v/parser/tmpl.v +++ b/vlib/v/parser/tmpl.v @@ -67,7 +67,7 @@ pub fn (mut p Parser) compile_template_file(template_file string, fn_name string source.writeln(' import strings // === vweb html template === -fn vweb_tmpl_${fn_name}() { +fn vweb_tmpl_${fn_name}() string { mut sb := strings.new_builder($lstartlength)\n ') @@ -237,6 +237,7 @@ mut sb := strings.new_builder($lstartlength)\n } source.writeln(parser.tmpl_str_end) source.writeln('_tmpl_res_$fn_name := sb.str() ') + source.writeln('return _tmpl_res_$fn_name') source.writeln('}') source.writeln('// === end of vweb html template ===') result := source.str() diff --git a/vlib/v/tests/tmpl_test.v b/vlib/v/tests/tmpl_test.v index e8cb5a9ea0..9c6a5e99e1 100644 --- a/vlib/v/tests/tmpl_test.v +++ b/vlib/v/tests/tmpl_test.v @@ -10,8 +10,21 @@ fn one() string { return $tmpl('tmpl/base.txt') } +fn outside_return() string { + name := 'Peter' + age := 25 + numbers := [1, 2, 3] + downloads := { + 'vlang/ui': '3201' + 'vlang/vtl': '123' + } + ignored := true + result := $tmpl('tmpl/base.txt') + return result +} + fn test_tmpl() { - assert one().trim_space() == "name: Peter + expected := "name: Peter age: 25 numbers: [1, 2, 3] @@ -41,6 +54,9 @@ this is not ignored so, it's basically true" + + assert one().trim_space() == expected + assert outside_return().trim_space() == expected } fn test_tmpl_in_anon_fn() {