Closed Mr0grog closed 9 months ago
Exceptions are not good: it would be better if .
(dot) would always evaluate to the current item, regardless of its position in the context stack, at the root or not.
And yes, the spec needs a clarification on that.
Is there a process for proposing the change? Do I just open a PR?
I think the right thing here is to add the .
language from the interpolation spec to the sections spec: “A single period (.
) indicates that the item currently sitting atop the context stack should be used”
And add this test to it, as well:
- name: Implicit Iterator - Root-level
desc: Implicit iterators should work on root-level lists.
data: [ { value: 'a' }, { value: 'b' } ]
template: '"{{#.}}({{value}}){{/.}}"'
expected: '"(a)(b)"'
Not sure if it makes sense to add test(s) to the interpolation spec, too, e.g:
- name: Dotted Names - Single Period
desc: A single period should resolve to the top of the context stack.
data:
name: 'Alice'
template: '{{#name}}({{.}}){{/name}}'
expected: '(Alice)'
- name: Dotted Names - Root-level Single Period
desc: A single period at the root level should resolve to the passed-in data.
data: 'Alice'
template: '"{{.}}"'
expected: '"Alice"'
In particular, I can imagine that last one might not be reasonable for a lot of implementations.
Any updates or feedback on this? Anyone?
The repo maintainers have not given a sign of life for years.
:(
This does not mean that Hogan itself is not maintained!
Oh, for sure. I have an actual PR sitting (equally long) over there.
@Mr0grog We have active maintenance again and I encourage you to submit a pull request with the clarifications that you have in mind.
Oh wow, I haven’t thought about this issue in ages! Will try to bring myself back up to speed on this and submit something when I have time. (If someone else watching is more actively experiencing this and is more up-to-speed, feel free to take this on.)
Mustache.php does the "right thing" with the previous examples, as well as this monstrosity:
- name: Shenanigans
desc: This is silly, but should be allowed.
data: [ 'a', 'b' ]
template: '"{{#.}}({{.}}){{/.}}"'
expected: '"(a)(b)"'
… to be honest I don't hate it? I mean, you shouldn't use it. But it's consistent. I don't hate that it's possible.
@jgonggrijp OK, I added a PR for this in #176. I changed the spec language in a slightly different way from what I proposed here, but I think it’s better. The two extra tests I suggested adding to the "Interpolation" spec already exist now, so I skipped them (not sure if I missed them before or if they are new).
Can the implicit iterator be used to iterate over a list if that list is the root level item on the context stack? e.g. should this:
{{#.}} {{name}} {{/.}}
work with this data:[{name: Alice}, {name: Bob}]
to generate:Alice Bob
?From a reading of the interpolation spec it sounds like it should:
But there’s actually no mention of implicit iterators in the descriptive part of the sections spec and the tests it contains do not cover this case. It just so happens that some implementations also have conflicting behavior here.
I recently finished switching all of a client’s Mustache.js-rendered templates to being precompiled with Hogan.js. In Mustache, the scenario described above works fine, but in Hogan it does not. In a true test of Murphy’s Law, I ran afoul of this issue during the switch.
It would be great to get some clarification in the tests themselves and, ideally, in the descriptive part of the spec as well.
Hogan has a long-standing bug for this; some clarity from this spec would probably go a long way towards resolving the issue, either by forcing Hogan to change through failing tests or by clarifying that behavior isn’t one Hogan needs to support.