8000 Add a way to acceed to loop data by bubbatls · Pull Request #323 · janl/mustache.js · GitHub
[go: up one dir, main page]

Skip to content

Conversation

bubbatls
Copy link

when using block {{#anArray}} {{/anArray}}
You may accedd to loop datas with @index @Arrayindex and @Length
{{@index}} / {{@Length}}
will output
1 / 12
2 / 12
3 / 12
...
@Arrayindex start at 0 instead of 1

Maybe there is a better way to do this, but something is really missing on this point

when using block {{#anArray}} {{/anArray}}
You may accedd to loop datas with @index @Arrayindex and @Length
{{@index}} / {{@Length}}
will output
1 / 12
2 / 12
3 / 12
...
@Arrayindex start at 0 instead of 1
@jrmax
Copy link
jrmax commented Feb 10, 2014

+1
this would be great!

@rcampbel
Copy link

This looks like LOGIC
-1

@dgmike
Copy link
dgmike commented Mar 17, 2014

Nice 👍

@dasilvacontin
Copy link
Collaborator

Maybe a helper function/plugin.

@dgmike
Copy link
dgmike commented Dec 22, 2014

Is there in spec?

@Arrayindex
Copy link

I am @Arrayindex

@rcampbel
Copy link
rcampbel commented Nov 1, 2017

So, just out of curiosity, this branch only handles when the value is an object.

  • Are there going to be unit tests that go along with this change?
  • What would the side effects be when the value is truthy/falsy??

Example
Data :

{
  "booleanValue": true,    // true
  "stringValue": "Some String",    // true
  "emptyString": "",    // false
  "whiteSpace": "    ",    // true
  "numericFalseValue": 0,    // false
  "numericTrueValue": 1    // true
}

Template:

{{#booleanValue}} {{@index}} / {{@length}} {{/booleanValue}}
{{#stringValue}} {{@index}} / {{@length}} {{/stringValue}}
{{#emptyString}} {{@index}} / {{@length}} {{/emptyString}}
{{#whiteSpace}} {{@index}} / {{@length}} {{/whiteSpace}}
{{#numericFalseValue}} {{@index}} / {{@length}} {{/numericFalseValue}}
{{#numericTrueValue}} {{@index}} / {{@length}} {{/numericTrueValue}}
  • How would this work with nested data??

Example
Data :

{
  employees : [
    {
      hasLeftCompany : false,
      firstName: "John"
    },
    {
      hasLeftCompany : true,
      firstName: "Jane"
    }
  ]
}

Template:

<table>
  <thead>
    <tr>
      <th colspan="2">Active Employees</th>
    </tr>
    <tr>
      <th></th>
      <th>First Name</th>
    </tr>
  </thead>
  <tbody>
    {{#employees}}
      {{^hasLeftCompany}}
        <tr>
          <td>{{@index}}</td>
          <td>{{fisrtName}}</td>
        </tr>
      {{/hasLeftCompany}}
    {{/employees}}  
  </tbody>
</table>

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

Successfully merging this pull request may close these issues.

6 participants
0