#2586·knockout

Question: sprintf like usage

Author: dakhnodCreated Jan 30, 2022Updated Feb 1, 2022

I, as a Knockout beginner, am facing this challenge: I have a data object

{
  "status": {
    "completion": 0.8,
    "percent": 80
  }
}

And want to give the user the ability to define his own template strings like "{status.percent}%, {status.completion}". After the conversion, this should result in 80%, 0.8. The data object aswell as my template string are handled by javascript. Ideal would be some function like replaceVariables(formatString, dataObject).

  • How can I offload as much replacement work as possible to Knockout?
  • What's the best format for the template string?
  • Does Knockout support this style of binding declaration without data-bind?
  • What would be some example code doing this?
  • Should I maybe Just grep {.*} from the template string myself, and write the object mapping logic myself? AFAIK Knockout uses data-bind attributes, which differ from my application a bit, since I don't want to make the user write HTML...

Thanks for the great work so far, and for the support.