Skip to content

Numbers, null and booleans

JPVenson edited this page May 10, 2022 · 10 revisions

Build in Types

Morestachio allows a growing number of c# like Build in types. Currently the following types are build in:

Numbers

With Morestachio you can directly write certain number types into the template. Since version 3.0 morestachio has support for following numbers:

  • int: {{100}} any number smaller then +-2147483648
  • uint {{100U}} or any number between +2147483648 and +4294967295
  • long: {{100L}} or any number between +-4294967296 and +-9223372036854775807
  • ulong {{100UL}} or any number between +9223372036854775808 and +18446744073709551615
  • float {{100F}} or {{100.0F}} up to any number +-3.40282346638528859e+38 with precision of ~6-9 digits
  • double {{100D}} or {{1000.0}} default number up to any number +-1.79769313486232E+308 with precision of ~15-17 digits
  • decimal {{100M}} or {{1000.0M}} up to any number +-79228162514264337593543950335 with precision of 28-29 digits (please see https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types)

any literal is case-insensitive.

You can ether write them as the root of an value like {{123.123.()}} or as an argument {{Path.To.Format(123)}}

There is build in support for several number operations that includes but is not limited to:

  • Add(Number)
  • Substract(Number)
  • Multiply(Number)
  • Divide(Number)
  • Modulo(Number)
  • ShiftLeft(Number)
  • ShiftRight(Number)
  • BiggerAs(Number)
  • SmallerAs(Number)
  • Abs()
  • Negate()
  • Log(Number)
  • Max(Number)
  • Min(Number)
  • Pow(Number)
  • Equals(Number)
  • Same(Number)

The number class is implicitly convertable from and to all c# number types (excluding BigInteger). That means you can create an formatter and accept a Number as an parameter like:

[MorestachioFormatter("Format", "")
public static Number Format(Number left)
{
	...
}

This formatter can be called on ether an in-template create number like {{123.Format()}} but also on an data supplied number like

var data = new {
   val = 500D
};
var template = "{{val.Format()}}";

As all c# numbers are convertable to the Number class, you can call all formatters on all numbers within the template:

var data = new {
   val = 500D
};
var template = "{{val.Add(123)}}";

or

var template = "{{123.Add(val)}}";

Strings

You can define a string value by starting an expression with " or ' you must end the string with the same character the string has started with. You can use the string as the root of an expression such as {{"test"}} and {{'test'}} or you can use a string to call a formatter as {{data.CallFormatter("test")}}. You can even call a formatter on the expressed string like: {{"test".CallFormatter()}}

Boolean

You can define a boolean value by simply writing {{true}} or {{false}}. You can call formatters on the result or use the values as part of an formatter {{data.CallFormatter(true)}}

Null

Just as the boolean type you can also express a Null value. You cannot call a formatter on null. If you attempt to print a null value, it will be substituted with the ParserOptionsBuilder.WithNull(string). You can overwrite the ParserOptionsBuilder.WithNull(string) value for your template by setting the $null variable like this:

{{not.existing.path}} <-- this will print ParserOptions.Null
{{#var $null = "I AM NULL"}}
{{not.existing.path}} <-- this will print whatever is set by $null