mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
This commit is contained in:
parent
3e9b06031c
commit
3f8aa77990
@ -127,7 +127,18 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
|
||||
pos: start_pos.extend(p.prev_tok.pos())
|
||||
}
|
||||
}
|
||||
literal_string_param := if is_html { '' } else { p.tok.lit }
|
||||
mut literal_string_param := if is_html { '' } else { p.tok.lit }
|
||||
if p.tok.kind == .name {
|
||||
if var := p.scope.find_var(p.tok.lit) {
|
||||
if var.expr is ast.StringLiteral {
|
||||
literal_string_param = var.expr.val
|
||||
}
|
||||
} else if var := p.scope.find_const(p.mod + '.' + p.tok.lit) {
|
||||
if var.expr is ast.StringLiteral {
|
||||
literal_string_param = var.expr.val
|
||||
}
|
||||
}
|
||||
}
|
||||
path_of_literal_string_param := literal_string_param.replace('/', os.path_separator)
|
||||
mut arg := ast.CallArg{}
|
||||
if !is_html {
|
||||
|
45
vlib/v/tests/tmpl_using_variable_or_const_path_test.v
Normal file
45
vlib/v/tests/tmpl_using_variable_or_const_path_test.v
Normal file
@ -0,0 +1,45 @@
|
||||
const template_path = 'tmpl/template.in'
|
||||
|
||||
pub struct SomeThing {
|
||||
pub:
|
||||
m map[string]string
|
||||
}
|
||||
|
||||
fn (s SomeThing) someval(what string) string {
|
||||
return s.m[what]
|
||||
}
|
||||
|
||||
fn (s SomeThing) template_variable() string {
|
||||
path := 'tmpl/template.in'
|
||||
return $tmpl('tmpl/template.in')
|
||||
}
|
||||
|
||||
fn (s SomeThing) template_const() string {
|
||||
return $tmpl('tmpl/template.in')
|
||||
}
|
||||
|
||||
fn test_tmpl_with_variable_path() {
|
||||
mut sm := map[string]string{}
|
||||
sm['huh'] = 'monkey'
|
||||
sm['hah'] = 'parrot'
|
||||
|
||||
s := SomeThing{sm}
|
||||
result := s.template_variable()
|
||||
println(result)
|
||||
assert result.trim_space() == 'someval: monkey
|
||||
|
||||
someotherval: parrot'
|
||||
}
|
||||
|
||||
fn test_tmpl_with_const_path() {
|
||||
mut sm := map[string]string{}
|
||||
sm['huh'] = 'monkey'
|
||||
sm['hah'] = 'parrot'
|
||||
|
||||
s := SomeThing{sm}
|
||||
result := s.template_const()
|
||||
println(result)
|
||||
assert result.trim_space() == 'someval: monkey
|
||||
|
||||
someotherval: parrot'
|
||||
}
|
Loading…
Reference in New Issue
Block a user