Skip to content
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

Open
pistacchio opened this issue Aug 11, 2010 · 16 comments
Open

{{.}} not working #10

pistacchio opened this issue Aug 11, 2010 · 16 comments

Comments

@pistacchio
Copy link

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?

@dclowd9901
Copy link

+1

1 similar comment
@agdolla
Copy link

agdolla commented Aug 21, 2012

+1

@deazurain
Copy link

It would be really nice to have this basic feature.

@aduley
Copy link

aduley commented Nov 20, 2012

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;
  }

  ...

@deazurain
Copy link

I decided to use the javascript parser of Moustache itself and pass file contents to it through nodejs

@raycmorgan
Copy link
Owner

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.

@deazurain
Copy link

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.

@raycmorgan
Copy link
Owner

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.

@deazurain
Copy link

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.

@raycmorgan
Copy link
Owner

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.

@deazurain
Copy link

I have to say that that is a very elegant solution. It is still a workaround for not having "." though ;)

@allevo
Copy link

allevo commented Jan 6, 2014

+1

@dclowd9901
Copy link

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.

@atk
Copy link

atk commented Jul 13, 2015

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;
}

@predhme
Copy link

predhme commented Mar 22, 2017

+1

1 similar comment
@sonnyboy27
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants