Is possible to convert from external string source to string template?


#1

Hello everybody,
As topic’s title, is there any solution for this problem?
I want to load string from html template file in assets folder and it should be modify (eg: fill data to form) before display to view. So how can convert html string to template string in kotlin or any solution for my problem.

Thanks for your time.


#2

Kotlin string templates are always processed statically at compile time. There are plenty of existing template languages (Velocity, Freemarker etc.) that you can use for substituting values into a template at runtime.


#3

While we are talking about template engines, I want to suggest Rythm Template Engine. It has a really nice Razor-like syntax:

<ul>
@for(Product product: products) {    
     <li>@product.getName(). Price: @product.getPrice().format("## ###,00") €</li>
}
</ul>

#4

Glad I found this post. I work on the MyBatis project, and I’ve been exploring the use of Kotlin as an alternative scripting language. The fact that it only allows for templates to be processed at compile time makes it unusable for that purpose. MyBatis already has implementation for Velocity and Freemarker, so I’ll go back to working on my Groovy implementation using GString templates. Thanks for clarifying!