GithubHelp home page GithubHelp logo

{{.}} not working about mu HOT 16 OPEN

raycmorgan avatar raycmorgan commented on May 3, 2024 1
{{.}} not working

from mu.

Comments (16)

dclowd9901 avatar dclowd9901 commented on May 3, 2024

+1

from mu.

agdolla avatar agdolla commented on May 3, 2024

+1

from mu.

deazurain avatar deazurain commented on May 3, 2024

It would be really nice to have this basic feature.

from mu.

aduley avatar aduley commented on May 3, 2024

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.

deazurain avatar deazurain commented on May 3, 2024

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

from mu.

raycmorgan avatar raycmorgan commented on May 3, 2024

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.

deazurain avatar deazurain commented on May 3, 2024

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.

raycmorgan avatar raycmorgan commented on May 3, 2024

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.

deazurain avatar deazurain commented on May 3, 2024

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.

raycmorgan avatar raycmorgan commented on May 3, 2024

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.

deazurain avatar deazurain commented on May 3, 2024

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

from mu.

allevo avatar allevo commented on May 3, 2024

+1

from mu.

dclowd9901 avatar dclowd9901 commented on May 3, 2024

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.

atk avatar atk commented on May 3, 2024

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.

predhme avatar predhme commented on May 3, 2024

+1

from mu.

sonnyboy27 avatar sonnyboy27 commented on May 3, 2024

+1

from mu.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.