Use additional renderers like this class to implement the interface:
The code is as follows: |
Copy code |
Internal class AdvancedDateTimeRenderer: IAttributeRenderer { Public string ToString (object o) { Return ToString (o, null ); } Public string ToString (object o, string formatName) { If (o = null) Return null; If (string. IsNullOrEmpty (formatName )) Return o. ToString (); DateTime dt = Convert. ToDateTime (o ); Return string. Format ("{0:" + formatName + "}", dt ); } }
|
And then add this to your StringTemplate such as register the class to the template group:
The code is as follows: |
Copy code |
Var stg = new StringTemplateGroup ("Templates", path ); Stg. RegisterAttributeRenderer (typeof (DateTime), new AdvancedDateTimeRenderer ());
|
Statement in the then in st file Template file:
The code is as follows: |
Copy code |
$ YourDateVariable; format = "dd/mm/yyyy" $ |
The other method calls the attribute. ToString () method by default. To customize the output format, you must implement the IAttributeRenderer interface. The following defines a custom date formatting output:
The code is as follows: |
Copy code |
Class CustomDataRenderer: IAttributeRenderer { Public string ToString (object o) { If (o = null) Return null; String str = o. ToString (); DateTime dt; If (! DateTime. TryParse (o. ToString (), out dt )) Return str; Return dt. ToString ("MM dd, yyyy, HH: mm "); } Public string ToString (object o, string formatName) { Throw new NotImplementedException (); } } |
Usage:
The code is as follows: |
Copy code |
StringTemplate st = new StringTemplate ("$ d $ "); St. RegisterRenderer (typeof (DateTime), new CustomDataRenderer (); // register the default output format of the DateTime type in the current Template St. SetAttribute ("d", DateTime. Now ); Console. WriteLine (st. ToString ()); |
We can see that the format defined by the previously defined ToString (object o) method is output. If multiple formats need to be output, this method cannot be used.
Next, modify the CustomDataRenderer class so that it can output multiple formats:
The code is as follows: |
Copy code |
/// <Summary> /// Output the custom ST date format /// </Summary> Class CustomDataRenderer: IAttributeRenderer { /// <Summary> /// Default output method /// </Summary> Public string ToString (object o) { Return ToString (o, null ); } /// <Summary> /// Output method when the output format is specified /// </Summary> /// <Param name = "formatName"> format name </param> Public string ToString (object o, string formatName) { If (o = null) Return null; String str = o. ToString (); DateTime dt; If (! DateTime. TryParse (o. ToString (), out dt )) Return str; Switch (formatName ){ Case "CNDate ": Return dt. ToString ("MM dd, yyyy "); Case "CNTime ": Return dt. ToString ("HH: mm "); Default: Return dt. ToString ("MM dd, yyyy, HH: mm "); } } }
Usage: StringTemplate st = new StringTemplate ("[$ d1 $] [$ d2; format =" CNDate "$] [$ d3; format =" CNTime "$]"); St. RegisterRenderer (typeof (DateTime), new CustomDataRenderer (); // register the default output format of the DateTime type in the current Template St. SetAttribute ("d1", DateTime. Now ); St. SetAttribute ("d2", DateTime. Now ); St. SetAttribute ("d3", DateTime. Now ); Console. WriteLine (st. ToString ()); |
Output: [, January 1, April 30, 2010] [, April 30, 2010]