Formatting strings Help
I am trying to format a datetime and have tried a helper function but I am unable to get it to work. the datetime looks something like this "2011/07/08 07:00:00". I would like my template to do something the when I just want the date part, or the time part. {{formatDate DateTime}} {{formatTime DateTime}}
Thus resulting in 2011/07/08 and 07:00:00
Thanks for your help. DM
Need more info about what you tried and why it isn't working as you expected.
It is probably an issue more to do with my usage. This is what I have : Handlebars.RegisterHelper("formatDate", f_date); public static void f_date(TextWriter output, HelperOptions options, dynamic context, params object[] arguments) { output.Write(String.Format("{0:d}", context)); }
I am getting this error: Reached end of template before block expression was closed
The error you are getting means there is a syntax error in the actual template, unrelated to the helper.
There is a problem in your helper though, the "context" argument is the entire data object, it doesn't make any sense to pass it into string.format. The arguments array contains the items passed into the helper.
This is my template; {{formatDate myDateTime}} {{formatTime myDateTime}}
myDateTime = "2011/07/08 07:00:00"
Handlebars.RegisterHelper("formatDate", f_date); public static void f_date(TextWriter output, HelperOptions options, dynamic context, params object[] arguments) { output.Write(String.Format("{0:d}", arguments[0])); }
Can you point me to an example of how to do this? I understand that I have something incorrect, I changed it to arguments[0] same error.
Sorry about all the questions. Thanks for your patients.
Figured it out. Not sure if it is the most efficient way to do it but it worked. Handlebars.RegisterHelper("formatDate", (writer, context, args) => { DateTime dt = Convert.ToDateTime(args[0]); writer.Write(String.Format("{0:d}", dt)); }); Thanks for the hints.
With the following template and helper,
<div>
{{json AnyDataFromContext}}
</div>
Handlebars.RegisterHelper("json", ( writer, context, arguments ) =>
{
writer.Write("This will functionally break.");
});
the response will end in with the error: "Reached end of template before block expression was closed"
However, if i change both into the following (notice that the helper is no longer titled "json")
<div>
{{jsonXYZ AnyDataFromContext}}
</div>
Handlebars.RegisterHelper("jsonXYZ", ( writer, context, arguments ) =>
{
writer.Write("This functionally works.");
});
the result is
<div>
This functionally works.
</div>
Can you please open a new issue? This one was closed a year ago and doesn't appear to be related.
Generated issue: #118