Comments (3)
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.
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.
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)
- Compiled templates / Detection of recursion in partials is too restrictive HOT 8
- Emoji support HOT 1
- how can i use it in mvc4
- Nuget says no dependencies, but I get an error on dependency check HOT 1
- .net core support HOT 1
- Port to dotnet core HOT 2
- Windows 10 (WUP) Support? HOT 1
- Inverted evaluation for NULL/False column value? HOT 4
- How to add html between two tags HOT 6
- Wrong Umlauts in Render.StringToString HOT 7
- Nustache incorrectly escapes unicode characters
- Where are lambda's renderer and context gone in new syntax ? HOT 8
- XPath functions in templates HOT 1
- Moving to .NET Standard 2.0 HOT 10
- Custom helpers not available in Template HOT 1
- Add integer support in the method IsTrusthy() HOT 4
- Wrong order list HOT 2
- spaces in options
- Explicit CultureInfo specification HOT 3
- Please add Extensions Setting Support for system.text.json
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 nustache.