-
Notifications
You must be signed in to change notification settings - Fork 2
Template: named formatting
Regextra provides the static Template.Format
method to perform named formatting on object properties (including nested properties), Dictionary
objects, and dynamic objects. It's also accessible via the FormatTemplate
string extension method.
The formatter features:
- Basic formatting
- Nested properties formatting
- Dictionary formatting
- Escaping of delimiters
- Standard/Custom/IFormatProvider string formatting
- Detailed exception messages to pinpoint missing properties
Check out the comparison section later in this document to see how Regextra's Template stacks up to similar libraries!
var person = new { Name = "Ahmad" };
var template = "Hi {Name}!";
var result = Template.Format(template, person);
// Hi Ahmad!
Note: dynamic objects are also supported
The FormatTemplate
extension method extends string
objects.
var person = new { Name = "Ahmad" };
var template = "Hi {Name}!";
var result = template.FormatTemplate(person);
// Hi Ahmad!
var item = new
{
Name = "Ahmad",
Ide = new
{
Name = "Visual Studio",
Version = 2013
}
};
var template = "{Name}'s favorite IDE is: {Ide.Name} v{Ide.Version}!";
var result = Template.Format(template, item);
// Ahmad's favorite IDE is: Visual Studio v2013!
var item = new Dictionary<string, object>()
{
{ "Name", "Ahmad" },
{ "Item", true }
};
var template = "Hello {Name}! The item is {Item}.";
var result = Template.Format(template, item);
// Hello Ahmad! The item is True.
The {
and }
delimiters can be escaped by doubling them up, similar to the .NET String.Format
approach.
var person = new { Name = "Ahmad" };
var template = "{{Name}} is: {Name}";
var result = Template.Format(template, person);
// {Name} is: Ahmad
The same .NET formatting types that you're familiar with can be used with Regextra's Template
.
To supply a standard or custom format, append a colon and the desired format inside the delimited property. In other words, use the {PropertyName:format}
syntax.
-
"{Date:t}".FormatTemplate(new { Date = DateTime.Now })
returns 8:45 PM -
"{Date:M/d/yyyy}".FormatTemplate(new { Date = DateTime.Now })
returns 3/4/2014 -
"{Number:C}".FormatTemplate(new { Number = 1500 })
returns $1,500.00 -
"{Item:N}".FormatTemplate(new { Item = Guid.NewGuid() })
returns a Guid with dashes removed
An overload also exists to supply an IFormatProvider
:
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-GB");
var result = "This item costs {Price:C1}".FormatTemplate(item, NumberFormatInfo.CurrentInfo);
// This item costs £1,234.6
An invalid format will cause a FormatException
to be thrown, since this is implemented by using the String.Format
method.
When an object is missing a property used in the template, a MissingFieldException
will be thrown and will contain a helpful message to clarify the missing property.
public class Person
{
public string Name { get; set; }
}
var person = new Person { Name = "Ahmad" };
var result = "Hi {XYZ}!".FormatTemplate(person);
// Field 'Person.XYZ' not found.
var person = new { Name = "Ahmad" };
var result = "Hi {XYZ}!".FormatTemplate(person);
// Field '<>f__AnonymousType0`1.XYZ' not found.
Named formatting was a cool idea I wanted to tackle. However, it turns out other people have had similar ideas. Regextra's Template stacks up great. This section exists to compare Regextra and show that it's in good company.
I set out to compare the same libraries that SmartFormat.NET compares itself to. The Benchmarks project shows the benchmark setup. Due to the lack of OSS licenses for those libraries, I've opted not to commit them with the project's code.
Note: if I'm mistaken about the features of any library, or its availability as an OSS library, please let me know.
Formatter | Non-Nested Properties | Nested Properties | Dynamic Objects | IDictionary Objects | Pluralisation | List Formatting |
---|---|---|---|---|---|---|
Regextra Template | Y | Y | Y | Y | - | - |
SmartFormat.NET | Y | Y | Y | Y | Y | Y |
Henri | Y | Y | - | - | - | - |
Haack | Y | Y | - | - | - | - |
James | Y | - | - | - | - | - |
Hanselman | Y | - | - | - | - | - |
Oskar | Y | - | - | Y | - | - |
The following benchmark results are ordered by best performance. Most of the top performers are fairly close to each other.
Note: Regextra, SmartFormat, and Haack formatters tied for 2nd place @ 14 ms.
- Henri: 9 ms -- 0.009 ms/iteration
- Regextra Template w/FastMember: 14 ms -- 0.014 ms/iteration
- SmartFormat.NET: 14 ms -- 0.014 ms/iteration
- Haack: 14 ms -- 0.014 ms/iteration
- SmartFormat.NET (Cached): 15 ms -- 0.015 ms/iteration
- Regextra Template w/Reflection: 16 ms -- 0.016 ms/iteration
- James: 21 ms -- 0.021 ms/iteration
- Hanselman: 35 ms -- 0.035 ms/iteration
- Oskar: 67 ms -- 0.067 ms/iteration
- Henri: 17 ms -- 0.017 ms/iteration
- Haack: 23 ms -- 0.023 ms/iteration
- Regextra Template w/FastMember: 25 ms -- 0.025 ms/iteration
- SmartFormat.NET: 26 ms -- 0.026 ms/iteration
- SmartFormat.NET (Cached): 27 ms -- 0.027 ms/iteration
- Regextra Template w/Reflection: 29 ms -- 0.029 ms/iteration
- Regextra Template: 8 ms -- 0.008 ms/iteration
- SmartFormat.NET: 11 ms -- 0.011 ms/iteration
- Oskar: 31 ms -- 0.031 ms/iteration
- Regextra Template: 12 ms -- 0.012 ms/iteration
- SmartFormat.NET: 15 ms -- 0.015 ms/iteration
- Regextra Template w/FastMember: 23 ms -- 0.023 ms/iteration
- SmartFormat.NET: 30 ms -- 0.03 ms/iteration
- Phil Haack's Named Formats Redux
- Oskar Austegard's C#: String.Inject() - Format strings by key tokens
- James Newton-King's FormatWith 2.0 - String formatting with named variables