Helpers for types
var tpl = Handlebars.Compile("{{x}}");
var o = new { x = new List<int>() {1,2,3}};
Console.WriteLine(tpl(o));
returns ToString() of my list (System.Collections.Generic.List`1[System.Int32]). How it would be possible to assign default renderer/helper for types?
Why not something like this?
static void Main(string[] args)
{
var handlebars = Handlebars.Create();
var template = handlebars.Compile( "{{x}}" );
var data = new
{
x = new SuperList<int>
{
1,
2,
3
}
};
Console.WriteLine( template( data ) );
Console.ReadLine();
}
public class SuperList<T> : List<T>
{
public override string ToString()
{
return "default value";
}
}
Does handlebarsjs allow for custom value renderers?
In my use case end users write templates themselves. And I can't extend all classes just to override ToString(); @rexm, looks like handlebarsjs doesn't have this, but it might be not that important in JS, because JS toString() renders collections to coma separated items.
If you are strongly against type helpers, then how about passing custom EncodedTextWriter? Is that something you would accept as pull request?
I am not sure about this because it's adding a more complicated way to solve a problem that is already solvable - use partials, or just express directly in the temple how you want the details of your complex object rendered.
My key points are:
- Seeing
System.Collections.Generic.List1[System.Int32]gibberish in rendered template looks not right - Using hardcoded
value.ToString()(and internalEncodedTextWriter) limits extensibility - Handlebarsjs renders nice
[1,2,"x","y"]from collection but Handlebars.Net rendersSystem.Collections.Generic.List1[System.Object] - Allowing to specify custom
IEncodedTextWriterorFunc<object,string>inHandlebarsConfigurationshould not add any complexity or performance penalty.
IMHO library should be as extensible, as possible and I shouldn't need to fork for such a simple thing. Forget about type helpers, but please think about interchangeable EncodedTextWriter. I will make pull request if you are willing to accept it :)
The purpose of this library is to provide a pure c# implementation of the handlebarsjs library... Rather than adding custom extension points that don't exist in handlebars, I would start more conservatively and agree that we could add in standard rendering for IEnumerable to uplift c#'s native stringify for collections/arrays/etc to match JavaScript/Handlebars. That is a gap in parity we should resolve.
The purpose of this library is to provide a pure c# implementation of the handlebarsjs library
Thats a fine argument against my type helpers proposal. But why don't you want to decouple EncodedTextWriter or at least value.ToString()? It has nothing to do with handlebarjs implementation, it's just a design decision.. By the way, Views are not pure c# implementation of handlebarjs :)
It sounds like the problem is a parity gap between c#'s default stringification of IEnumerable and handlebars' stringification of arrays/collections, so fixing that should address the issue.
Strigification of IEnumerable will lessen my pains. But you didn't answer my question: why not decouple strigification and let your library user override default behavior? All mature serialization/rendering lib's allows that. I need custom DateTime (it might be even more important to customise DateTime than other types) rendering, I need custom IEnumerable rendering and so on, and so far.. And I fail to see how allowing that deviates from porting objective..
Adding additional features beyond parity requires more critical thinking and hard work for the people (mainly me) who will maintain this library for the rest of its life. Nowhere did I say we won't do any of those things, but they require a level of thoughtfulness that fixing the intial parity gap does not. So we'll start there. But to be fully transparent, I am very wary of changes that go much beyond what the JS library supports, and probably won't add them.
you can create a helper if you want to format values..
eg:
handlebars.RegisterHelper( "format",
( writer, context, arguments ) =>
{
try
{
if ( arguments.Length <= 1 )
return;
var format = "";
if ( arguments.Length > 1 )
format = arguments[1] as string ?? "";
//culture name
var culture = CultureInfo.InvariantCulture;
if ( arguments.Length > 2 )
{
var cultureName = arguments[2] as string;
if ( !String.IsNullOrEmpty( cultureName ) )
culture = new CultureInfo( cultureName );
}
var date = arguments[0] as DateTime?;
if ( date.HasValue )
{
//write output
writer.WriteSafeString( date.Value.ToString( format, culture ) );
return;
}
var number = arguments[0] as decimal?;
if ( number.HasValue )
{
writer.WriteSafeString( number.Value.ToString( format, culture ) );
return;
}
}
catch ( Exception ) { }
writer.Write( "Unsupported" );
} );
Hi, @JvanderStad. I know how to create helpers, that not what I need.
I need to be able to override strigification, which is now hardoced in file EncodedTextWriter.cs.
Reasons for this might be many, but mine is that I have no ability to change existing and future templates.
It sounds a little bit too much to try to change the library because you can't change the templates. Sorry but it seems you are trying to solve a wrong problem here. It's not up to library to vary string representations of different objects. That is what templates are for.
It's up to the user of library to vary string representations. Or to vary as many other things as reasonably possible, despite use cases foreseen by developer. As I said before, your arguments are very sound for "type helpers" proposal, but IMHO in no way valid for ToString() decoupling proposal. Just imagine JSON.NET developers saying: "It's not up to library to vary string representations of different objects, just use your custom date objects".
I'm not a maintainer btw, just contributor
@rexm
The purpose of this library is to provide a pure c# implementation of the handlebarsjs library...
But library shall take into account differences between JS and .NET. In .NET there is no ability to override ToString method for structures.
I'm using NodaTime.Duration structure in template data. Duration.ToString uses some default format and current culture. I need another format. In JS I can just wrap/override default toString behaviour.
It would be helpfull to provide custom converters for types like it is done in JSON.NET (via attributes on types and via engine configuration). Such change doesn't violates handlebars specification, it adjusts implementation for .NET specifics.
The formatting use case is easily solved with the existing helper functionality.
Yes, but this will lead to different templates for different platforms (I'm using templates for both JS and .NET).
I recommend using a helper on both platforms to maintain parity.
Template is supposed to be controlled by user. This will look starnge that to render duration user have to use helper. Why don't you want to introduce this change? I can help with the implementation.