From 123d788d0d9a50c7af6e97e3d44818b00d9ddd1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Wed, 10 Jun 2020 18:53:04 +0200 Subject: [PATCH] vweb: include directive --- vlib/vweb/tmpl/tmpl.v | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/vlib/vweb/tmpl/tmpl.v b/vlib/vweb/tmpl/tmpl.v index 3ee4b07956..6012dcc9c5 100644 --- a/vlib/vweb/tmpl/tmpl.v +++ b/vlib/vweb/tmpl/tmpl.v @@ -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 == '' { 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