vweb: include directive

pull/5338/head
Daniel Däschle 2020-06-10 18:53:04 +02:00 committed by GitHub
parent a2d7bc6e6f
commit 123d788d0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 14 deletions

View File

@ -29,14 +29,6 @@ enum State {
pub fn compile_template(content, fn_name string) string {
// lines := os.read_lines(path)
mut html := content
mut header := ''
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
}
lines := html.split_into_lines()
mut s := strings.new_builder(1000)
// base := path.all_after_last('/').replace('.html', '')
@ -44,11 +36,7 @@ pub fn compile_template(content, fn_name string) string {
import strings
// === vweb html template ===
fn vweb_tmpl_${fn_name}() {
mut sb := strings.new_builder(${lines.len * 30})
header := \' \' // TODO remove
_ = header
//footer := \'footer\'
")
mut sb := strings.new_builder(${lines.len * 30})\n")
s.writeln(str_start)
mut state := State.html
mut in_span := false
@ -65,7 +53,18 @@ pub fn compile_template(content, fn_name string) string {
else if line == '</script>' {
state = .html
}
if line.contains('@if ') {
if line.contains('@include ') {
pos := line.index('@include ') or {
continue
}
mut file_name := line[pos + 9..]
file_path := os.join_path('templates', '${file_name}.html')
mut file_content := os.read_file(file_path) or {
panic('reading file $file_name failed')
}
file_content = file_content.replace("\'", '"')
s.writeln(file_content)
} else if line.contains('@if ') {
s.writeln(str_end)
pos := line.index('@if') or {
continue