Skip to content

Creating Your Own Extension

axunonb edited this page Jul 14, 2026 · 2 revisions

A Hello World Example

Implement IFormatter

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

	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