fmt: handle $tmpl

pull/6968/head
Alexander Medvednikov 2020-11-26 18:53:38 +01:00
parent 5efd393af2
commit 5e59718970
3 changed files with 9 additions and 6 deletions

View File

@ -807,7 +807,11 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
} }
ast.ComptimeCall { ast.ComptimeCall {
if node.is_vweb { if node.is_vweb {
f.write('$' + 'vweb.html()') if node.method_name == 'html' {
f.write('\$vweb.html()')
} else {
f.write("\$tmpl('$node.args_var')")
}
} else { } else {
f.write('${node.left}.\$${node.method_name}($node.args_var)') f.write('${node.left}.\$${node.method_name}($node.args_var)')
} }

View File

@ -64,7 +64,6 @@ fn (mut p Parser) vweb() ast.ComptimeCall {
if !is_html { if !is_html {
p.check(.string) p.check(.string)
} }
println('SSSS "$s"')
p.check(.rpar) p.check(.rpar)
// Compile vweb html template to V code, parse that V code and embed the resulting V function // Compile vweb html template to V code, parse that V code and embed the resulting V function
// that returns an html string. // that returns an html string.
@ -92,12 +91,11 @@ fn (mut p Parser) vweb() ast.ComptimeCall {
} }
// println('path is now "$path"') // println('path is now "$path"')
} }
if true || p.pref.is_verbose { if p.pref.is_verbose {
println('>>> compiling comptime template file "$path"') println('>>> compiling comptime template file "$path"')
} }
tmp_fn_name := p.cur_fn_name.replace('.', '__') tmp_fn_name := p.cur_fn_name.replace('.', '__')
v_code := tmpl.compile_file(path, tmp_fn_name) v_code := tmpl.compile_file(path, tmp_fn_name)
println('done')
$if print_vweb_template_expansions ? { $if print_vweb_template_expansions ? {
lines := v_code.split('\n') lines := v_code.split('\n')
for i, line in lines { for i, line in lines {
@ -108,7 +106,7 @@ fn (mut p Parser) vweb() ast.ComptimeCall {
start_pos: 0 start_pos: 0
parent: p.global_scope parent: p.global_scope
} }
if true || p.pref.is_verbose { if p.pref.is_verbose {
println('\n\n') println('\n\n')
println('>>> vweb template for $path:') println('>>> vweb template for $path:')
println(v_code) println(v_code)
@ -143,6 +141,7 @@ fn (mut p Parser) vweb() ast.ComptimeCall {
is_vweb: true is_vweb: true
vweb_tmpl: file vweb_tmpl: file
method_name: n method_name: n
args_var: s
} }
} }

View File

@ -2,7 +2,7 @@ fn one() string {
name := 'Peter' name := 'Peter'
age := 25 age := 25
numbers := [1, 2, 3] numbers := [1, 2, 3]
return $tmpl('tmpl/1.txt') return $tmpl('vlib/v/tests/tmpl/1.txt')
} }
fn test_tmpl() { fn test_tmpl() {