mustache / spec

The Mustache spec.
MIT License
364 stars 71 forks source link

Clarification: Interpolation Context Sigil #74

Closed KentonWhite closed 10 months ago

KentonWhite commented 10 years ago

I'm looking for clarification about using '.' as the context sigil in interpolation. The spec says

1) Split the name on periods; the first part is the name to resolve, any remaining parts should be retained. ... 5) If any name parts were retained in step 1, each should be resolved against a context stack containing only the result from the former resolution. If any part fails resolution, the result should be considered falsey, and should interpolate as the empty string.

I'm working with a Mustache implementation in the R language. R allows for '.' in variable names. It uses '$' and '@' as context sigils. This puts me in a bind. To be compliant with the Mustache spec, I must split names on periods and then resolve each remaining piece within the previous context. Doing this in R returns empty strings for valid R expressions, e.g. {{data.frame$foo}} will return the empty string regardless if data.frame exists on the current stack or if the name foo is valid in the context of data.frame .

Since Mustache is meant to be language agnostic, can I interpret "split the name on periods" to mean "split the name on context sigils"?

groue commented 10 years ago

You hit a point.

A very common interpretation of "language-agnostic", for Mustache, is that a single template file can be parsed and interpreted by various implementations written in different programming languages (ruby, python, java, etc.).

For instance, the tags {{variable}}, {{>partial}}, {{#section}}...{{/section}} etc. have the same meaning, and this is enforced by the spec.

However, the spec does not enforce that a single input (data + template) should render the same in all implementations. The weak spot is the way booleans are handled (see issue #4). In some languages, 0 (zero) would be truthy, and in other languages, the same value would be falsey.

In this last context, "language-agnostic" has a different interpretation: programming languages are free to interpret values according to their "natural" meaning.

With such a fuzzy definition of "language-agnostic", you are free to decide your favorite interpretation :-)

Don't let a specification turn your library into a unusable piece of software. Write a nice template engine for R. Whether it is called Mustache or not is not that much important.

KentonWhite commented 10 years ago

Thanks for the advice!

groue commented 10 years ago

Happy templating!

jgonggrijp commented 10 months ago

I somewhat disagree with the above advice; if an implementation uses $ instead of . as the name separator, then templates targeting that implementation will not be portable to other implementations and vice versa. That said, if portability is not a goal, then $ might indeed be the best for R.

Closing now as this question no longer seems to looking for an answer.