Skip to content

2.0 Planning #294

Closed
Closed

Description

@rexm

As a celebration of Handlebars.Net's millionth installation, it seems like a good time for:

Handlebars.Net Version 2.0 🎉

This thread will be to discuss what goes into 2.0. Really appreciate everyone's involvement, even if it's just bringing your experience and input to the table.

Call for Input

So far, here's the changes being considered (in no particular order): As consensus develops in this thread, approved items will get a [x] to indicate they're officially part of the 2.0 roadmap. Please review, comment, and add your own:

Special thanks

to @mcintyre321, @Magentaize, @dejx, @amaclean, @vzwick, @axelheer, @sandorfr, @leniency, @pmccloghrylaing, @mikeprince3, @StefH, @huysentruitw, @abraham-fox, @ondrejtomcik, @mohd-akram, @EBMike, @JvanderStad, @katavasha, @Andrew-Hanlon, @nblumhardt, @crra, @esskar, and the many others who've helped make this library useful to people all over the world in the last 5 years!

Activity

  1. pinned this issue on Mar 30, 2019
  2. rexm commented on Apr 27, 2019

    @rexm
    MemberAuthor

    Plan to address #292

  3. rexm commented on Apr 27, 2019

    @rexm
    MemberAuthor

    Plan to address #297

  4. rexm commented on May 26, 2019

    @rexm
    MemberAuthor

    Plan to address #301

  5. johnknoop commented on Jun 22, 2019

    @johnknoop

    Suggestion for v2:

    I'm not sure if this is even possible using the tools available, but here's my idea:

    We only use Handlebars to render html e-mails, and I imagine that's what most people use this library for, since rendering website UI's using Handlebars in .NET seems like an unusual thing to do. And given the nature of e-mail clients, a common setup is to produce the e-mail body in this sequence:

    1. Check if the template is already compiled. If not, compile it.
    2. Render the template.
    3. Run Premailer.Net or any other CSS inliner.
    4. Extract image paths from img elements in the html, and create CID-embedded attachments.

    Now, step 3 and 4 doesn't really have any dependency on the context data used in step 2. Therefore, it's a shame we can't do step 3 and 4 as part of the compilation, so it only has do be done once per template. Now I have no idea how the execution flow looks when it comes compiling a Handlebars template, but it would be nice if one were able to register hooks that get called by Handlebars.Compile:

    Handlebars.RegisterCompileHook(CompileHooks.AfterAllPartialsHaveBeenIncluded, (string html, Dictionary<string, object> templateProperties) =>
    {
        templateProperties["EmbeddedImages"] = ExtractImagePaths(html, "/path/of/templates");
        return ApplyCssInlining(html);
    });

    And the return type of Handlebars.Compile would be:

    interface CompiledTemplate
    {
        string Render(object context);
    
        // Any properties that have been set by hooks
        Dictionary<string, object> templateProperties)
    }

    With this approach, the task of rendering an e-mail body would be so much quicker since all that has to be done is step 2 instead of steps 2, 3 and 4.

    I don't know enough about the internals of how Handlebars templates get compiled. But if this seems reasonable and possible to do, then I'd be happy to help out implement it. But I don't want to add it to the checklist above before I know if this is even a sensible thing to do and if it's inline with the vision of this project.

  6. rexm commented on Jun 22, 2019

    @rexm
    MemberAuthor

    The main goal of this project is to have a .NET native implementation of the handlebars JS library. The handful of times we’ve bent and added features that aren’t in the original handlebars library, it’s ended up being a mistake. That being said, since .NET and JS are different platforms, it can make sense to diverge. It should make sense based on those differences and be universally useful - contrary to your perception, while generating emails is one popular use-case, it is also widely used as a website templating engine, a code generator (not something I’d recommend, but it gets used that way) and a lot of others. Compiler hooks could make sense, but we’d need to work on a model that is useful to everyone and doesn’t commit us to supporting contracts we end up regretting.

  7. johnknoop commented on Jun 22, 2019

    @johnknoop

    Maybe compiler hooks could be part of the extensibility model you mention above. But I don't even know if html in -> html out would make sense as a contract for such hooks. You might work with other primitives than strings of html during the assembly process?

    Also, everything I've written is based on the assumption that partials are sucked in during compilation, and not later on when the compiled template is rendered. Is this a correct assumption btw?

  8. rexm commented on Jun 22, 2019

    @rexm
    MemberAuthor

    When a template is compiled and a partial is referenced, that reference is replaced by a function to look up the partial by name and invoke it during execution. What are you thinking you could do with that?

  9. johnknoop commented on Jun 23, 2019

    @johnknoop

    In the e-mail scenario, you typically wanna apply the CSS inlining after all partials have been pulled together into a single html document, because you might have styles coming from one partial and the elements from another. I just assumed this was how the compilation process looked, meaning the result of the compilation was a complete hbs document where all partials have been sucked, and the only thing happening during rendering was turning the remaining handlebars expressions into strings using the context data.

    So given this, perhaps there's no real use for compilation hooks in my case.

  10. rexm commented on Jun 23, 2019

    @rexm
    MemberAuthor

    Have you considered inlining before template compilation? Even as part of the build...

  11. johnknoop commented on Jun 23, 2019

    @johnknoop

    I have, The problem is that styles might reside in a partial for reusability, so it's only when a template has been assembled into a complete html file that I can safely apply the inlining.

  12. rexm commented on Jun 23, 2019

    @rexm
    MemberAuthor

    I suggest exploring some other structures to achieve the same result without introducing the same challenges.

  13. 17 remaining items

  14. bags1976 commented on Jun 12, 2020

    @bags1976

    The handlebars api has a method to parse the ast/tokens returned prior to invoking the compile.

    https://github.com/handlebars-lang/handlebars.js/blob/master/docs/compiler-api.md

    This would be very useful to allow a user to extract the tokens from a template and then be able to extract the variables required for a particular template. The feature of extracting the variables was requested long ago here in the native handlebars.js and ultimately is what I would love to see implemented as well.

    handlebars-lang/handlebars.js#1207

    Currently there is no way to have the compiler parse the template for tokens and simply return the results in the interface and they are internal classes / methods so there is no good way to extract this information now.

  15. elkhawajah commented on Sep 9, 2020

    @elkhawajah

    The v1.0 is not compatible with .net core 2.2 right ? what is the alternatives for a compatible version for a production app ? Seems v2.0 not officially released yet.

    Thoughts please.

  16. StefH commented on Sep 9, 2020

    @StefH
    Collaborator

    The current Handlebars.net does support NETStandard 2.0 so it can be used by .net core 2.2

  17. elkhawajah commented on Sep 9, 2020

    @elkhawajah
  18. oformaniuk commented on Oct 16, 2020

    @oformaniuk
    Member

    Suggestion for v2:

    I'm not sure if this is even possible using the tools available, but here's my idea:

    We only use Handlebars to render html e-mails, and I imagine that's what most people use this library for, since rendering website UI's using Handlebars in .NET seems like an unusual thing to do. And given the nature of e-mail clients, a common setup is to produce the e-mail body in this sequence:

    1. Check if the template is already compiled. If not, compile it.
    2. Render the template.
    3. Run Premailer.Net or any other CSS inliner.
    4. Extract image paths from img elements in the html, and create CID-embedded attachments.

    Now, step 3 and 4 doesn't really have any dependency on the context data used in step 2. Therefore, it's a shame we can't do step 3 and 4 as part of the compilation, so it only has do be done once per template. Now I have no idea how the execution flow looks when it comes compiling a Handlebars template, but it would be nice if one were able to register hooks that get called by Handlebars.Compile:

    Handlebars.RegisterCompileHook(CompileHooks.AfterAllPartialsHaveBeenIncluded, (string html, Dictionary<string, object> templateProperties) =>
    {
        templateProperties["EmbeddedImages"] = ExtractImagePaths(html, "/path/of/templates");
        return ApplyCssInlining(html);
    });

    And the return type of Handlebars.Compile would be:

    interface CompiledTemplate
    {
        string Render(object context);
    
        // Any properties that have been set by hooks
        Dictionary<string, object> templateProperties)
    }

    With this approach, the task of rendering an e-mail body would be so much quicker since all that has to be done is step 2 instead of steps 2, 3 and 4.

    I don't know enough about the internals of how Handlebars templates get compiled. But if this seems reasonable and possible to do, then I'd be happy to help out implement it. But I don't want to add it to the checklist above before I know if this is even a sensible thing to do and if it's inline with the vision of this project.

    @johnknoop, starting from 2.0 this scenario would be achievable using low-level IExpressionMiddleware interface. Interface allows transformations on Expression Tree before it is compiled. In order to get desired behaviour you'd need to implement custom ExpressionVisitor to walk the tree and perform steps 3 and 4.

  19. StefH commented on Oct 16, 2020

    @StefH
    Collaborator

    Maybe already in place : make sure the library is signed.

  20. StefH commented on Nov 4, 2020

    @StefH
    Collaborator

    @zjklee
    Maybe add a config option to "keep the replacement as-is if the value cannot be replaced"

    Like:

    string source = @"My name is {{Name}} and I work at {{Job}}";
    var template = Handlebars.Compile(source);
    var data = new {
        Job = "Walmart"
    };
    var result = template(data);

    Result should be:

    My name is {{Name}} and I work at Walmart
    

    https://stackoverflow.com/questions/64379082/handlebars-net-how-to-change-behavior-if-data-does-not-exist

  21. oformaniuk commented on Nov 4, 2020

    @oformaniuk
    Member
  22. StefH commented on Nov 4, 2020

    @StefH
    Collaborator

    @zjklee This looks perfect!
    Maybe you can answer that StackOverflow question? The credit is yours ;-)

  23. sferencik commented on Dec 2, 2020

    @sferencik

    @zjklee what's your ETA on this? We're keen to start using some of the new features; should we wait or build our own pre-release version?

  24. oformaniuk commented on Dec 2, 2020

    @oformaniuk
    Member

    @sferencik originally I was planning to address #97 before publishing a release version as it would introduce a dramatic breaking change to the public API. However it looks like I'd defer it to 3.0 release.
    Regarding ETA: I'm planning to release one more pre-release version today/tomorrow after #391 is merged. This pre-release package would be the last one before I publish release version unless any critical bugs are found. I guess you can expect a release version by the end of the week.

  25. sferencik commented on Dec 6, 2020

    @sferencik

    That would be great, thanks! Is there something I can help with?

  26. oformaniuk commented on Dec 6, 2020

    @oformaniuk
    Member

    That would be great, thanks! Is there something I can help with?

    I'm trying to keep the list of issues up to date so in case you want to contribute I'd definitely be happy to accept PRs.

  27. oformaniuk commented on Dec 6, 2020

    @oformaniuk
    Member

    Release 2.0.0 is out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions