Handlebars.Net icon indicating copy to clipboard operation
Handlebars.Net copied to clipboard

Formatting strings Help

Open dmason511 opened this issue 11 years ago • 8 comments

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

dmason511 avatar Apr 20 '15 16:04 dmason511

Need more info about what you tried and why it isn't working as you expected.

rexm avatar Apr 20 '15 16:04 rexm

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

dmason511 avatar Apr 20 '15 17:04 dmason511

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.

rexm avatar Apr 20 '15 17:04 rexm

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.

dmason511 avatar Apr 20 '15 18:04 dmason511

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.

dmason511 avatar Apr 20 '15 20:04 dmason511

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>

alittlerabbi avatar May 18 '16 16:05 alittlerabbi

Can you please open a new issue? This one was closed a year ago and doesn't appear to be related.

rexm avatar May 18 '16 16:05 rexm

Generated issue: #118

alittlerabbi avatar May 18 '16 17:05 alittlerabbi