Skip to content

Commit

Permalink
vweb: .html('custom_template.html')
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Nov 26, 2023
1 parent c532ee6 commit 51bb276
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions examples/vweb/custom.html
@@ -0,0 +1 @@
<b>custom template</b>
4 changes: 4 additions & 0 deletions examples/vweb/vweb_example.v
Expand Up @@ -46,6 +46,10 @@ pub fn (mut app App) index() vweb.Result {
return $vweb.html()
}

pub fn (mut app App) custom_template() vweb.Result {
return $vweb.html('custom.html')
}

pub fn (mut app App) show_text() vweb.Result {
return app.text('Hello world from vweb')
}
Expand Down
18 changes: 14 additions & 4 deletions vlib/v/parser/comptime.v
Expand Up @@ -159,7 +159,8 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
pos: start_pos.extend(p.prev_tok.pos())
}
}
mut literal_string_param := if is_html { '' } else { p.tok.lit }
has_string_arg := p.tok.kind == .string
mut literal_string_param := if is_html && !has_string_arg { '' } else { p.tok.lit }
if p.tok.kind == .name {
if var := p.scope.find_var(p.tok.lit) {
if var.expr is ast.StringLiteral {
Expand All @@ -173,7 +174,12 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
}
path_of_literal_string_param := literal_string_param.replace('/', os.path_separator)
mut arg := ast.CallArg{}
if !is_html {
if is_html && !(has_string_arg || p.tok.kind == .rpar) {
p.error('expecting `\$vweb.html()` for a default template path or `\$vweb.html("/path/to/template.html")`')
}
if is_html && p.tok.kind != .string {
// $vweb.html() can have no arguments
} else {
arg_expr := p.expr(0)
arg = ast.CallArg{
expr: arg_expr
Expand Down Expand Up @@ -209,13 +215,17 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
fn_path := p.cur_fn_name.split('_')
fn_path_joined := fn_path.join(os.path_separator)
compiled_vfile_path := os.real_path(p.scanner.file_path.replace('/', os.path_separator))
tmpl_path := if is_html { '${fn_path.last()}.html' } else { path_of_literal_string_param }
tmpl_path := if is_html && !has_string_arg {
'${fn_path.last()}.html'
} else {
path_of_literal_string_param
}
// Looking next to the vweb program
dir := os.dir(compiled_vfile_path)
mut path := os.join_path_single(dir, fn_path_joined)
path += '.html'
path = os.real_path(path)
if !is_html {
if !is_html || has_string_arg {
if os.is_abs_path(tmpl_path) {
path = tmpl_path
} else {
Expand Down
6 changes: 3 additions & 3 deletions vlib/vweb/vweb.v
Expand Up @@ -235,9 +235,9 @@ pub fn (mut ctx Context) send_response_to_client(mimetype string, res string) bo
return true
}

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

Expand Down

0 comments on commit 51bb276

Please sign in to comment.