Ambient context, a.k.a. helpers #156
Replies: 2 comments 4 replies
-
In my implementation you can pass in a starting stack of context objects so this doesn't really affect mustache.java. I have used this strategy in a lot of places including for things like i18n. I'm not sure though how this would affect the specification though? It seems like any implementation could have an initialization path that affords this functionality. |
Beta Was this translation helpful? Give feedback.
-
Mustache.php has done this since forever. I think of it as an implementation detail of the engine, not really something that would belong in the template language spec. Though it might make sense to have a collection of "probably makes sense to implement things this way" somewhere? |
Beta Was this translation helpful? Give feedback.
-
Lambdas are provided to a template together with other input data. This approach is flexible and generally works well. However, it is easy to imagine that one might want to use a particular lambda in every template in a large application, for example one that interfaces with an i18n framework. It would be convenient if such a lambda could be registered in a central place to make it automatically available in every template. In Handlebars parlance, such a lambda would be referred to as a helper.
This central place does not need to restricted to lambdas. I can also imagine something like an
itemize
filter residing there (see #153), as well as frequenly used constants. As such, this central place is made of roughly the same substance as any context frame. Indeed, one could think of it as a context frame that is automatically inserted below the root context for every template that is rendered. Hence: ambient context.I suggest placing the ambient context below the root context, so that template users can still locally override helpers if there is a need to do so.
How to register a helper in the ambient context is implementation-defined. Implementations might ship with a small library of ready-to-use helpers, but I think actually adding those to the context should be opt-in.
Beta Was this translation helpful? Give feedback.
All reactions