Skip to content

Commit

Permalink
tmpl: add a tmpl_all_in_one_folder.vv test; fix for static_vars_in_tr…
Browse files Browse the repository at this point in the history
…anslated_mode.vv
  • Loading branch information
spytheman committed Feb 28, 2021
1 parent e564269 commit 4076e8e
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion 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
}
Expand Down
8 changes: 8 additions & 0 deletions vlib/v/tests/inout/file.md
@@ -0,0 +1,8 @@
@include './header.md'

This is the main content:
-------------------------
@content
-------------------------

@include './footer.md'
1 change: 1 addition & 0 deletions vlib/v/tests/inout/footer.md
@@ -0,0 +1 @@
my footer
1 change: 1 addition & 0 deletions vlib/v/tests/inout/header.md
@@ -0,0 +1 @@
my header
8 changes: 8 additions & 0 deletions 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
8 changes: 8 additions & 0 deletions 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())
}
11 changes: 6 additions & 5 deletions vlib/vweb/tmpl/tmpl.v
Expand Up @@ -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 {
Expand All @@ -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 := ''
Expand Down Expand Up @@ -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.')
Expand Down

0 comments on commit 4076e8e

Please sign in to comment.