From 70c18fc7b2d55a63a3cd19b60bf145e81c047820 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Sun, 7 Jun 2020 21:26:47 +1000 Subject: [PATCH] vweb: tmpl access to action method vars --- examples/vweb/index.html | 5 ++--- examples/vweb/vweb_example.v | 1 + vlib/v/parser/comptime.v | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/examples/vweb/index.html b/examples/vweb/index.html index 1fbd30b049..4391eeb248 100644 --- a/examples/vweb/index.html +++ b/examples/vweb/index.html @@ -1,5 +1,4 @@ -test app - +Test app
- +

@hello


diff --git a/examples/vweb/vweb_example.v b/examples/vweb/vweb_example.v index 0476db7ddd..e73de3b7f3 100644 --- a/examples/vweb/vweb_example.v +++ b/examples/vweb/vweb_example.v @@ -28,6 +28,7 @@ pub fn (mut app App) json_endpoint() { pub fn (mut app App) index() { app.cnt++ //app.vweb.text('Hello world from vweb') + hello := 'Hello world from vweb' $vweb.html() } diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index fd7fa10133..acbcfbd0de 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -113,6 +113,22 @@ fn (mut p Parser) vweb() ast.ComptimeCall { } } */ + // copy vars from current fn scope into vweb_tmpl scope + for stmt in file.stmts { + if stmt is ast.FnDecl { + fn_decl := stmt as ast.FnDecl + if fn_decl.name == 'vweb_tmpl' { + body_scope := file.scope.innermost(fn_decl.body_pos.pos) + for _, obj in p.scope.objects { + if obj is ast.Var { + v := obj as ast.Var + body_scope.register(v.name, *v) + } + } + break + } + } + } return ast.ComptimeCall{ is_vweb: true vweb_tmpl: file