Coder Social home page Coder Social logo

Comments (10)

BorisMoore avatar BorisMoore commented on June 7, 2024

The behavior of {{#each dataExpression}} ... {{/each}} or {{each dataExpression tmpl="myTemplate"}} is to render the content of the template against the data returned by dataExpression.

If the data is an array, it iterates for each item - which becomes the current dataItem for that rendered template (view) - and concatenates the result. But if dataExpression returns a single object, then it renders just once, with that object as current data item.

If you don't pass any data at all, as in {{each tmpl="myTemplate"}} then it will render the template just once, keeping the current dataItem from the outer scope.

I am considering changing the name of the tag from each to {{for}} to make less surprising that it does not iterate when you pass it a singleton.

It is not a common scenario to render the same template against each of the properties of an object. Usually you know, statically, the properties of the object, and can do

{{#each propertiesObject }}
    Created Date: {{=createdDat}} 
    Created By: {{=createdBy}} 
{{/each}}

If you want to apply the same template to each property, as an iteration over all the properties, you can provide a helper function to return the array, rather than add a new array to your data, as in {{#each $ctx.propertyArray(properties)}}...{{/each}}

See also this comment on my blog

from jsrender.

BorisMoore avatar BorisMoore commented on June 7, 2024

See also #49

from jsrender.

quasipickle avatar quasipickle commented on June 7, 2024

My apologies for necro-posting, but I wanted to provide an example of why someone might want to iterate through the properties of an object.

In a nutshell, for the purposes of data-linking, an array/collection does not allow (or very easily allow) for changing just one property of one entry.

Take this data:

inout = {
    joe:{
        name:  'Joe',
        status: 'out'},
    jane:{
        name: 'Jane',
        status:'in'},
    jill:{
        name:'Jill',
        status:'away'}
}

And I render it like so:
$("#domElement").link(inout,"#templateID");

I could then update a particular person like
$.observable(inout).setProperty({joe:{status:'in'}});

I think - I haven't tested this, but looking through the source, I'm guessing this is how it works

By only being able to iterate over arrays, I either need to

  1. Re-generate the entire array server side (assuming the change is triggering an ajax request to save in a DB or something), or
  2. Convert the inout object to an array, and keep a mapping of people to array index, so I can update the property like so:
    var index = nameToNameMap['joe'];
    $.observable(inout).setProperty({index:{status:'in'}});

Is there another way to approach this?

from jsrender.

BorisMoore avatar BorisMoore commented on June 7, 2024

I agree there are scenarios where this is useful. I hope to be able to look into supporting it...

from jsrender.

RobinDaugherty avatar RobinDaugherty commented on June 7, 2024

Can we reopen this issue then please? It's definitely necessary, and the original poster closed the ticket for some reason.

from jsrender.

BorisMoore avatar BorisMoore commented on June 7, 2024

I may provide built-in support for this in the future, but already it is quite easy to address this scenario using a custom helper function or tag. I have added a sample to show how to do that. (Code here)

from jsrender.

RobinDaugherty avatar RobinDaugherty commented on June 7, 2024

That looks perfect. Thanks!

from jsrender.

stewartlord avatar stewartlord commented on June 7, 2024

Any chance of reopening this issue? It took me a while to figure out hashes are not supported by {{for}}. Cool that you can do it via custom helpers, but this seems rather fundamental.

from jsrender.

BorisMoore avatar BorisMoore commented on June 7, 2024

OK, makes sense. Reopening as a feature request for built-in support.

from jsrender.

BorisMoore avatar BorisMoore commented on June 7, 2024

This feature is now supported, using the new {{props}} tag.
https://github.com/BorisMoore/jsrender/blob/master/demos/scenarios/03_iterating-through-fields-scenario.html
http://borismoore.github.io/jsrender/demos/scenarios/03_iterating-through-fields-scenario.html
#218

{{props details}}
        <div>
                <b>{{>key}}</b>: {{>prop}}
        </div>
{{/props}}

from jsrender.

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.