1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vweb: fix duplicate fn error with multiple templates

This commit is contained in:
Alexander Medvednikov 2020-06-09 12:43:16 +02:00
parent 1396dc1c89
commit b3e416fb52
4 changed files with 23 additions and 7 deletions

16
vlib/strings/strings.v Normal file
View File

@ -0,0 +1,16 @@
module strings
//import rand
// random returns a random string with `n` characters
/*
// TODO
pub fn random(n int) string {
buf := vmalloc(n)
for i in 0..n {
buf[i] = rand.next()
}
return tos(buf)
}
*/

View File

@ -12,7 +12,7 @@ fn (g &Gen) comptime_call(node ast.ComptimeCall) {
if stmt is ast.FnDecl {
fn_decl := stmt as ast.FnDecl
// insert stmts from vweb_tmpl fn
if fn_decl.name == 'vweb_tmpl' {
if fn_decl.name.starts_with('vweb_tmpl') {
g.stmts(fn_decl.stmts)
break
}

View File

@ -100,7 +100,7 @@ fn (mut p Parser) vweb() ast.ComptimeCall {
}
// println('path is now "$path"')
}
v_code := tmpl.compile_file(path)
v_code := tmpl.compile_file(path, p.cur_fn_name)
mut scope := &ast.Scope{
start_pos: 0
parent: p.global_scope
@ -117,7 +117,7 @@ fn (mut p Parser) vweb() ast.ComptimeCall {
for stmt in file.stmts {
if stmt is ast.FnDecl {
fn_decl := stmt as ast.FnDecl
if fn_decl.name == 'vweb_tmpl' {
if fn_decl.name.starts_with('vweb_tmpl') {
body_scope := file.scope.innermost(fn_decl.body_pos.pos)
for _, obj in p.scope.objects {
if obj is ast.Var {

View File

@ -12,14 +12,14 @@ const (
)
// compile_file compiles the content of a file by the given path as a template
pub fn compile_file(path string) string {
pub fn compile_file(path, fn_name string) string {
mut html := os.read_file(path) or {
panic('html failed')
}
return compile_template(html)
return compile_template(html, fn_name)
}
pub fn compile_template(content string) string {
pub fn compile_template(content, fn_name string) string {
// lines := os.read_lines(path)
mut html := content
mut header := ''
@ -36,7 +36,7 @@ pub fn compile_template(content string) string {
s.writeln("
import strings
// === vweb html template ===
fn vweb_tmpl() {
fn vweb_tmpl_${fn_name}() {
mut sb := strings.new_builder(${lines.len * 30})
header := \' \' // TODO remove
_ = header