-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
{{.}} not working #10
Comments
+1 |
1 similar comment
+1 |
It would be really nice to have this basic feature. |
Testing if the context is a string does the trick. All tests still pass following the change. // renderer.js
function contextLevelContains(context, fullPath) {
var pathParts = fullPath.split('.');
var obj = context;
// add this
if (typeof context === "string") {
return context;
}
... |
I decided to use the javascript parser of Moustache itself and pass file contents to it through nodejs |
The "." is not part of the mustache spec. It breaks the idea of logic-less, since now your templates have to know that you are dealing with an array of simple items. I might add this still, but the proper way of doing this is to either a) transform the input into an array of proper objects, or b) add a function to the view to do that at render time. |
From the mustache website: "We call it "logic-less" because there are no if statements, else clauses, or for loops. Instead there are only tags. " Isn't mustache ment to make your life easier? I think that wrapping an array into an object is actually less intuïtive than being able to use an array directly. For example: linking a set of stylesheets in your html file would be much nicer with the "." syntax. |
Mustache is suppose to make your templates logic free (not make your life necessarily easier right away), which in turn sometimes means there is more upfront work. This is what makes sure you don't start littering your templates with things it should not be concerned with. This limitation is what makes your life easier, since you can't get lazy and smash code into a hard to test template. When I have time I will implement this since it is wanted. First on my list is Lambda support though, since I need it and it is a huge thing missing from the spec. If someone makes a patch, I will take a look at it. Probably only need to mess around in the renderer. |
Personally, I don't think "." is related to program logic. You have to know what kind of data object you are dealing with anyway in any piece of mustache. Be it an object with certain fields or in this case an array of strings. Ofcourse you don't have to agree with me as it is your project. Good luck on the Lambda support! For now I will continue to use the javascript version of mustache and feed it strings that I load from disk with node. |
Sounds good. As a note here is how I would implement the stylesheet thing you mentioned. Although it is more work, it is easy and very flexible.
In your template:
The way I prefer doing this is having an object of helpers with functions like |
I have to say that that is a very elegant solution. It is still a workaround for not having "." though ;) |
+1 |
So, your solution works, raycmorgan, but now you're either a) going to have to do this logic for every instance in which you're doing array output or b) you're going to create a singleton helper class or something akin to it (essentially a layer between, say, underscore and Mu). Of course you want to keep things DRY, so you're going to want to do B, but then that means you've got this weird helper class littering your source code. Looping an array of values isn't outside the norm. If the model dictates the template (which is the idea of logic-less templates), then the model should very closely resemble the template. Voodoo like referring to a Helper class to get you the rest of the way there is antithetical to that theme. At the bare minimum, we need to be able to read out the values of a JSON structure without having to resort to using middleware. |
Proposed change to support this: renderer.js:124
|
+1 |
1 similar comment
+1 |
maybe i missed it, but when iterating through a simple array like [1, 2, 3, 4], shouldn't {{.}} indicate the current item? how to handle unnamed items?
The text was updated successfully, but these errors were encountered: