Skip to content

[Suggestion] Fallback to instance/static method if helper cannot be resolved #336

@oformaniuk

Description

@oformaniuk

Problem

Sometimes it's required to use some well-known methods inside of the template (like string.ToUpper()) and in order to do so you need to register new helper for each of such occurrences.

Solution

Right now there are two stages for helper binding:

  • compile time: helper is registered before template is compiled
  • late bind: helper is registered after template is compiled

I propose to add a third step, controlled by feature toggle in settings, that would try to resolve and invoke:

  • instance method of the object
  • static method of the type (if instance method was not found)

Adding the feature toggle is essential for a few reasons:

  • potential unexpected behavior due to lack of support in Handlebarsjs (see section below)
  • potential performance degradation (may be resolved by adding Compile<ExpectedModelType>() mentioned in 2.0 Planning #294 )

Support in Handlebarsjs

There was a discussion (handlebars-lang/handlebars.js/issues/168) regarding this functionality. The proposal was rejected. However for JS there's workaround mentioned in the same thread - you can override the handler. Unfortunately such overrides are not available in current implementation mostly because of .NET nature.

Useful for

The functionality may be useful for people who does not have strong performance requirements but require more feature reach and flexible templating engine. One of the examples may be generation of deployment artifacts (e.g. ARM template parameters generation).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions