jehugaleahsa / mustache-sharp

An extension of the mustache text template engine for .NET.
The Unlicense
306 stars 80 forks source link

Calculation in identifier #84

Open last-Programmer opened 5 years ago

last-Programmer commented 5 years ago

Is it possible to do expressions in identifier like {{num1 * num2}}. Adding this feature would make this library more powerful like xslt transformation.

Thanks

blakepell commented 1 year ago

Sharing for posterity:

The facility to do this exists on the C# side but that doesn't help if you want to do it in the tags. Currently, #set doesn't allow you to set the value. The solution could be a number of things.

  1. Some combination of allowing set to set a value also #set value 1 and then custom tags that perform the operations (perhaps an AddTagDefinition, etc.). That requires new tags for every operation (+, -, /, *) and somewhat boxes you in with the number of parameters accepted.
  2. My idea (that I might try out) is to create an eval TagDefinition that uses a scripting engine to evaluate the text (Lua or JavaScript being my initial thoughts, either through MoonSharp, Jint or Topaz). You could share the scopes state with the scripting engine and then do "{{#eval}}num1 * num2{{/eval}}" etc.