diff --git a/vlib/v/checker/tests/static_vars_in_translated_mode.vv b/vlib/v/checker/tests/static_vars_in_translated_mode.vv index e3abc22a96..4c8b38a607 100644 --- a/vlib/v/checker/tests/static_vars_in_translated_mode.vv +++ b/vlib/v/checker/tests/static_vars_in_translated_mode.vv @@ -1,6 +1,6 @@ [unsafe] fn counter() int { - static mut icounter := 0 + static icounter := 0 icounter++ return icounter } diff --git a/vlib/v/tests/inout/file.md b/vlib/v/tests/inout/file.md new file mode 100644 index 0000000000..ba3fc2f445 --- /dev/null +++ b/vlib/v/tests/inout/file.md @@ -0,0 +1,8 @@ +@include './header.md' + +This is the main content: +------------------------- +@content +------------------------- + +@include './footer.md' diff --git a/vlib/v/tests/inout/footer.md b/vlib/v/tests/inout/footer.md new file mode 100644 index 0000000000..fa5713a15b --- /dev/null +++ b/vlib/v/tests/inout/footer.md @@ -0,0 +1 @@ +my footer diff --git a/vlib/v/tests/inout/header.md b/vlib/v/tests/inout/header.md new file mode 100644 index 0000000000..171d4b5b27 --- /dev/null +++ b/vlib/v/tests/inout/header.md @@ -0,0 +1 @@ +my header diff --git a/vlib/v/tests/inout/tmpl_all_in_one_folder.out b/vlib/v/tests/inout/tmpl_all_in_one_folder.out new file mode 100644 index 0000000000..649f5915af --- /dev/null +++ b/vlib/v/tests/inout/tmpl_all_in_one_folder.out @@ -0,0 +1,8 @@ +my header + +This is the main content: +------------------------- +some string +------------------------- + +my footer diff --git a/vlib/v/tests/inout/tmpl_all_in_one_folder.vv b/vlib/v/tests/inout/tmpl_all_in_one_folder.vv new file mode 100644 index 0000000000..30aaf6971d --- /dev/null +++ b/vlib/v/tests/inout/tmpl_all_in_one_folder.vv @@ -0,0 +1,8 @@ +fn abc() string { + content := 'some string' + return $tmpl('file.md') +} + +fn main() { + print(abc()) +} diff --git a/vlib/vweb/tmpl/tmpl.v b/vlib/vweb/tmpl/tmpl.v index 5c328faf80..853ef9c3f6 100644 --- a/vlib/vweb/tmpl/tmpl.v +++ b/vlib/vweb/tmpl/tmpl.v @@ -13,8 +13,9 @@ const ( // compile_file compiles the content of a file by the given path as a template pub fn compile_file(path string, fn_name string) string { + basepath := os.dir(path) html := os.read_file(path) or { panic('html failed') } - return compile_template(html, fn_name) + return compile_template(basepath, html, fn_name) } enum State { @@ -24,7 +25,7 @@ enum State { // span // span.{ } -pub fn compile_template(html_ string, fn_name string) string { +pub fn compile_template(basepath string, html_ string, fn_name string) string { // lines := os.read_lines(path) mut html := html_.trim_space() mut header := '' @@ -81,19 +82,19 @@ _ = footer file_ext = '.html' } file_name = file_name.replace(file_ext, '') - mut templates_folder := 'templates' + mut templates_folder := os.join_path(basepath, 'templates') if file_name.contains('/') { if file_name.starts_with('/') { // absolute path templates_folder = '' } else { // relative path, starting with the current folder - templates_folder = './' + templates_folder = os.real_path(basepath) } } file_path := os.real_path(os.join_path(templates_folder, '$file_name$file_ext')) $if trace_tmpl ? { - eprintln('>>> @include line: "$line" , file_name: "$file_name" , file_ext: "$file_ext" , templates_folder: "$templates_folder" , file_path: "$file_path"') + eprintln('>>> basepath: "$basepath" , fn_name: "$fn_name" , @include line: "$line" , file_name: "$file_name" , file_ext: "$file_ext" , templates_folder: "$templates_folder" , file_path: "$file_path"') } file_content := os.read_file(file_path) or { panic('Vweb: Reading file $file_name failed.')