tmpl: add a tmpl_all_in_one_folder.vv test; fix for static_vars_in_translated_mode.vv

pull/9017/head
Delyan Angelov 2021-02-28 19:14:19 +02:00
parent e564269477
commit 4076e8eaa0
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
7 changed files with 33 additions and 6 deletions

View File

@ -1,6 +1,6 @@
[unsafe] [unsafe]
fn counter() int { fn counter() int {
static mut icounter := 0 static icounter := 0
icounter++ icounter++
return icounter return icounter
} }

View File

@ -0,0 +1,8 @@
@include './header.md'
This is the main content:
-------------------------
@content
-------------------------
@include './footer.md'

View File

@ -0,0 +1 @@
my footer

View File

@ -0,0 +1 @@
my header

View File

@ -0,0 +1,8 @@
my header
This is the main content:
-------------------------
some string
-------------------------
my footer

View File

@ -0,0 +1,8 @@
fn abc() string {
content := 'some string'
return $tmpl('file.md')
}
fn main() {
print(abc())
}

View File

@ -13,8 +13,9 @@ 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, fn_name string) string { pub fn compile_file(path string, fn_name string) string {
basepath := os.dir(path)
html := os.read_file(path) or { panic('html failed') } html := os.read_file(path) or { panic('html failed') }
return compile_template(html, fn_name) return compile_template(basepath, html, fn_name)
} }
enum State { enum State {
@ -24,7 +25,7 @@ enum State {
// span // span.{ // 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) // lines := os.read_lines(path)
mut html := html_.trim_space() mut html := html_.trim_space()
mut header := '' mut header := ''
@ -81,19 +82,19 @@ _ = footer
file_ext = '.html' file_ext = '.html'
} }
file_name = file_name.replace(file_ext, '') 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.contains('/') {
if file_name.starts_with('/') { if file_name.starts_with('/') {
// absolute path // absolute path
templates_folder = '' templates_folder = ''
} else { } else {
// relative path, starting with the current folder // 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')) file_path := os.real_path(os.join_path(templates_folder, '$file_name$file_ext'))
$if trace_tmpl ? { $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 { file_content := os.read_file(file_path) or {
panic('Vweb: Reading file $file_name failed.') panic('Vweb: Reading file $file_name failed.')