Can i transform string in template expression or lambda expression in kotlin? -
can transform string in template expression or lambda expression in kotlin?
val tm = "x = $"+"x" val fn: (x: string) -> string = { -> tm} val str = fn("this x!!!")
need get
x = x!!!
why?: can receive templates, example, database ps: or suggestions
kotlin templates evaluated @ compile time - won't work.
you should use 3rd party template engine.
freemarker such engine format similar kotlin's own templating format:
val tm = "x = \${x}" fun fn (x: string) : string { val t = template("name", stringreader(tm), configuration(configuration.version_2_3_26)) val out = stringwriter() t.process(mapof("x" x) ,out) return out.tostring() } println (fn("this x!!!")) // x = x!!!
two notes:
- you won't able use
"$x"
on freemarker,"${x}"
$
sign can escaped in kotlin string using\$
wiki
Comments
Post a Comment