Giter VIP home page Giter VIP logo

Comments (3)

jdiamond avatar jdiamond commented on August 21, 2024

Never thought of this before so expected there wouldn't be a way, but I just wrote this test and it passed:

[Test]
public void It_can_render_arrays()
{
    var result = Render.StringToString("before{{#.}}{{.}}{{/.}}after", new[] { 1, 2, 3 });
    Assert.AreEqual("before123after", result);
}

The trick is knowing that {{.}} renders the current item in an array. I knew that would work. What I was surprised by was that it works as a key for sections, too.

Not sure if other Mustache implementations work the same way, but I'll close this unless you re-open it telling me it's wrong.

from nustache.

deeptechtons avatar deeptechtons commented on August 21, 2024

how did i ever forget that information awwwhh man. I need to writedown that . represents the current context when using mustache. thanks @jdiamond i also have another programming (but related to Nustache) question for a model like below
`Todo

  • Id
  • Name
  • ParentId`

For a List<Todo> I need to find the children for the current todo and render them with some identation. I came up with below pseudo template
{{#.}}<div><h3>Parent Todo</h3>{{Name}}NOW HOW TO RENDER CHILD TODO</div>{{/.}}
how do I find the child Todo for the current todo from the List<Todo> inside a Mustache template? children can be found using programming code like below but no ideas as to how inside a template
instance.FindAll(delegate(todo){ return todo.ParentId == theParent.Id; })

where theParent is the current parent element for which children are seeked

from nustache.

jdiamond avatar jdiamond commented on August 21, 2024

I'm afraid Mustache was purposely designed to avoid "polluting" your templates with that kind of code. You're going to need to transform your flat list of Todo objects into a hierarchical tree and use that tree from the templates. A lot of people call this creating a View Model or a Presentation Model. Mustache practically requires it for any non-trivial example.

I haven't had to do recursive templates with Mustache yet, so I tried to create an example and was really surprised (and a little disgusted) with how difficult it was.

Take a look at Example_Tree.cs in the Nustache.Core.Tests project. The transformation from flat list to hierarchical tree happens in GetNodeTree(). The recursive template is in the test method and is pretty nasty. It also uses an extension to Mustache that, as far as I know, is only available in Nustache. If you need compatibility with other implementations, you can just move the inline template definition into an external template.

If you (or anybody else) knows how this can be improved, please let me know. This is a real bummer.

from nustache.

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.