Comments (16)
+1
from mu.
+1
from mu.
It would be really nice to have this basic feature.
from mu.
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;
}
...
from mu.
I decided to use the javascript parser of Moustache itself and pass file contents to it through nodejs
from mu.
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 mu.
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.
from mu.
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.
from mu.
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.
from mu.
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.
{ styles: ["foo.css", "bar.css"],
stylesheets: function () {
return this.styles.map(function (name) {
return {src: name};
});
}
}
In your template:
{{#stylesheets}} <link type="text/css" rel="style" href="{{href}}"> {{/stylesheets}}
The way I prefer doing this is having an object of helpers with functions like stylesheet
and merge that with the data being input. this way I can have "data" and "helpers" separate.
from mu.
I have to say that that is a very elegant solution. It is still a workaround for not having "." though ;)
from mu.
+1
from mu.
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.
from mu.
Proposed change to support this:
renderer.js:124
function walkToFind(context, name) {
var i = context.length;
if (name === '.' && context[i - 2] instanceof Array) {
return context[i - 1];
}
while (i--) {
var result = contextLevelContains(context[i], name);
if (result !== undefined) {
return result;
}
}
return undefined;
}
from mu.
+1
from mu.
+1
from mu.
Related Issues (20)
- How to output error code instead of missing partial? HOT 1
- Deprecated method: util.pump in new version of node
- Rendered data doesn't write correctly to HTTP stream. HOT 1
- Integration with express HOT 4
- Get Templates HOT 1
- Partials with relative path not implemented HOT 1
- "Error" event not being sent;
- Default file extension
- Memory leak due to infinite loop in recursive partials. HOT 1
- Silent failure when a template file does not exist
- feature request: custom delimiter HOT 1
- integrate with express? HOT 1
- partials with mu-cli.js HOT 4
- .write( string, encoding, offset, length is deprecated notice HOT 5
- No lambda support
- Write output to file HOT 1
- feature request: expose render context
- Is this repo dead? HOT 1
- External local stylesheets not loading
- Breaks on node 12, TypeError: util.puts is not a function
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mu.