Skip to content

Creating Your Own Extension

axunonb edited this page Mar 11, 2022 · 1 revision

A Hello World Example

Implement IFormatter

public class HelloFormatter : IFormatter
{
	public string Name { get; set; } = "hello";

	public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
	{
		var iCanHandleThisInput = formattingInfo.CurrentValue is bool;
		if (!iCanHandleThisInput)
			return false;

		formattingInfo.Write("HELLO ");
		if ((bool) formattingInfo.CurrentValue)
			formattingInfo.Write(formattingInfo.FormatterOptions);
		else
			formattingInfo.Write(formattingInfo.Format.GetLiteralText());

		return true;
	}
}

Example usage:

Smart.Default.AddExtensions(new HelloFormatter());

Smart.Format("{value:hello(world):earth}", new { value = true });
// Outputs: "HELLO world"

Smart.Format("{value:hello(world):earth}", new { value = false });
// Outputs: "HELLO earth"
Clone this wiki locally