vweb: fix duplicate fn error with multiple templates
							parent
							
								
									1396dc1c89
								
							
						
					
					
						commit
						b3e416fb52
					
				|  | @ -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) | ||||||
|  | } | ||||||
|  | */ | ||||||
|  | 
 | ||||||
|  | @ -12,7 +12,7 @@ fn (g &Gen) comptime_call(node ast.ComptimeCall) { | ||||||
| 			if stmt is ast.FnDecl { | 			if stmt is ast.FnDecl { | ||||||
| 				fn_decl := stmt as ast.FnDecl | 				fn_decl := stmt as ast.FnDecl | ||||||
| 				// insert stmts from vweb_tmpl fn
 | 				// 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) | 					g.stmts(fn_decl.stmts) | ||||||
| 					break | 					break | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
|  | @ -100,7 +100,7 @@ fn (mut p Parser) vweb() ast.ComptimeCall { | ||||||
| 		} | 		} | ||||||
| 		// println('path is now "$path"')
 | 		// 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{ | 	mut scope := &ast.Scope{ | ||||||
| 		start_pos: 0 | 		start_pos: 0 | ||||||
| 		parent: p.global_scope | 		parent: p.global_scope | ||||||
|  | @ -117,7 +117,7 @@ fn (mut p Parser) vweb() ast.ComptimeCall { | ||||||
| 	for stmt in file.stmts { | 	for stmt in file.stmts { | ||||||
| 		if stmt is ast.FnDecl { | 		if stmt is ast.FnDecl { | ||||||
| 			fn_decl := stmt as 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) | 				body_scope := file.scope.innermost(fn_decl.body_pos.pos) | ||||||
| 				for _, obj in p.scope.objects { | 				for _, obj in p.scope.objects { | ||||||
| 					if obj is ast.Var { | 					if obj is ast.Var { | ||||||
|  |  | ||||||
|  | @ -12,14 +12,14 @@ const ( | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // compile_file compiles the content of a file by the given path as a template
 | // 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 { | 	mut html := os.read_file(path) or { | ||||||
| 		panic('html failed') | 		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)
 | 	// lines := os.read_lines(path)
 | ||||||
| 	mut html := content | 	mut html := content | ||||||
| 	mut header := '' | 	mut header := '' | ||||||
|  | @ -36,7 +36,7 @@ pub fn compile_template(content string) string { | ||||||
| 	s.writeln(" | 	s.writeln(" | ||||||
| 	import strings | 	import strings | ||||||
| 	// === vweb html template ===
 | 	// === vweb html template ===
 | ||||||
| 	fn vweb_tmpl() { | 	fn vweb_tmpl_${fn_name}() { | ||||||
| 	mut sb := strings.new_builder(${lines.len * 30}) | 	mut sb := strings.new_builder(${lines.len * 30}) | ||||||
| 	header := \' \' // TODO remove | 	header := \' \' // TODO remove | ||||||
| 	_ = header | 	_ = header | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue