vweb: look for html templates in `templates/`

pull/5338/head
Alexander Medvednikov 2020-06-10 18:00:06 +02:00
parent 972f60d785
commit a2d7bc6e6f
2 changed files with 13 additions and 12 deletions

View File

@ -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

View File

@ -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