scalatemplatestemplate-engine

Scala support for string templates?


Is there default (in SDK) Scala support for string templating? Example: "$firstName $lastName"(named not numbered parameters) or even constructs like for/if. If there is no such default engine, what is the best scala library to accomplish this?


Solution

  • Complementing Kim's answer, note that Java's Formatter accepts positional parameters. For example:

    "%2$s %1$s".format(firstName, lastName)
    

    Also, there's the Enhanced Strings plugin, which allows one to embed arbitrary expressions on Strings. For example:

    @EnhanceStrings // enhance strings in this scope
    trait Example1 {
      val x = 5
      val str = "Inner string arithmetics: #{{ x * x + 12 }}"
    }
    

    See also this question for more answers, as this is really a close duplicate.