From a2d7bc6e6faf5c5fd6e12353bc9832a1662ced30 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 10 Jun 2020 18:00:06 +0200 Subject: [PATCH] vweb: look for html templates in `templates/` --- vlib/v/parser/comptime.v | 19 ++++++++++--------- vlib/vweb/tmpl/tmpl.v | 6 +++--- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 424086db24..79703dadb2 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -86,20 +86,21 @@ fn (mut p Parser) vweb() ast.ComptimeCall { p.check(.rpar) // Compile vweb html template to V code, parse that V code and embed the resulting V function // that returns an html string. - mut path := p.cur_fn_name + '.html' - // if p.pref.is_verbose { - println('>>> compiling vweb HTML template "$path"') - // } + html_name := '${p.cur_fn_name}.html' + // Looking next to the vweb program + dir := os.dir(p.scanner.file_path) + mut path := os.join_path(dir, html_name) if !os.exists(path) { - // Can't find the template file in current directory, - // try looking next to the vweb program, in case it's run with - // v path/to/vweb_app.v - path = os.dir(p.scanner.file_path) + '/' + path + // can be in `templates/` + path = os.join_path(dir, 'templates', html_name) if !os.exists(path) { - p.error('vweb HTML template "$path" not found') + p.error('vweb HTML template "$html_name" not found') } // println('path is now "$path"') } + // if p.pref.is_verbose { + println('>>> compiling vweb HTML template "$path"') + // } v_code := tmpl.compile_file(path, p.cur_fn_name) mut scope := &ast.Scope{ start_pos: 0 diff --git a/vlib/vweb/tmpl/tmpl.v b/vlib/vweb/tmpl/tmpl.v index d9e7367be0..3ee4b07956 100644 --- a/vlib/vweb/tmpl/tmpl.v +++ b/vlib/vweb/tmpl/tmpl.v @@ -30,9 +30,9 @@ pub fn compile_template(content, fn_name string) string { // lines := os.read_lines(path) mut html := content mut header := '' - if os.exists('header.html') && html.contains('@header') { - h := os.read_file('header.html') or { - panic('reading file header.html failed') + if os.exists('templates/header.html') && html.contains('@header') { + h := os.read_file('templates/header.html') or { + panic('reading file templates/header.html failed') } header = h.replace("\'", '"') html = header + html