Skip to content

Commit 51bb276

Browse files
committed
vweb: .html('custom_template.html')
1 parent c532ee6 commit 51bb276

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

examples/vweb/custom.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<b>custom template</b>

examples/vweb/vweb_example.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ pub fn (mut app App) index() vweb.Result {
4646
return $vweb.html()
4747
}
4848

49+
pub fn (mut app App) custom_template() vweb.Result {
50+
return $vweb.html('custom.html')
51+
}
52+
4953
pub fn (mut app App) show_text() vweb.Result {
5054
return app.text('Hello world from vweb')
5155
}

vlib/v/parser/comptime.v

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
159159
pos: start_pos.extend(p.prev_tok.pos())
160160
}
161161
}
162-
mut literal_string_param := if is_html { '' } else { p.tok.lit }
162+
has_string_arg := p.tok.kind == .string
163+
mut literal_string_param := if is_html && !has_string_arg { '' } else { p.tok.lit }
163164
if p.tok.kind == .name {
164165
if var := p.scope.find_var(p.tok.lit) {
165166
if var.expr is ast.StringLiteral {
@@ -173,7 +174,12 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
173174
}
174175
path_of_literal_string_param := literal_string_param.replace('/', os.path_separator)
175176
mut arg := ast.CallArg{}
176-
if !is_html {
177+
if is_html && !(has_string_arg || p.tok.kind == .rpar) {
178+
p.error('expecting `\$vweb.html()` for a default template path or `\$vweb.html("/path/to/template.html")`')
179+
}
180+
if is_html && p.tok.kind != .string {
181+
// $vweb.html() can have no arguments
182+
} else {
177183
arg_expr := p.expr(0)
178184
arg = ast.CallArg{
179185
expr: arg_expr
@@ -209,13 +215,17 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
209215
fn_path := p.cur_fn_name.split('_')
210216
fn_path_joined := fn_path.join(os.path_separator)
211217
compiled_vfile_path := os.real_path(p.scanner.file_path.replace('/', os.path_separator))
212-
tmpl_path := if is_html { '${fn_path.last()}.html' } else { path_of_literal_string_param }
218+
tmpl_path := if is_html && !has_string_arg {
219+
'${fn_path.last()}.html'
220+
} else {
221+
path_of_literal_string_param
222+
}
213223
// Looking next to the vweb program
214224
dir := os.dir(compiled_vfile_path)
215225
mut path := os.join_path_single(dir, fn_path_joined)
216226
path += '.html'
217227
path = os.real_path(path)
218-
if !is_html {
228+
if !is_html || has_string_arg {
219229
if os.is_abs_path(tmpl_path) {
220230
path = tmpl_path
221231
} else {

vlib/vweb/vweb.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@ pub fn (mut ctx Context) send_response_to_client(mimetype string, res string) bo
235235
return true
236236
}
237237

238-
// Response HTTP_OK with s as payload with content-type `text/html`
239-
pub fn (mut ctx Context) html(s string) Result {
240-
ctx.send_response_to_client('text/html', s)
238+
// Response HTTP_OK with payload with content-type `text/html`
239+
pub fn (mut ctx Context) html(payload string) Result {
240+
ctx.send_response_to_client('text/html', payload)
241241
return Result{}
242242
}
243243

0 commit comments

Comments
 (0)