DEV Community

Cover image for DativeJs {{#each}} block
Tobi
Tobi

Posted on • Edited on

2 2

DativeJs {{#each}} block

The each block in dativejs is used to iterate array|object

In the Formal Versions Of Dativejs

It Was Hard to Achieve This

So We Made this to be a betterment for that :)

Usage

<div>
  {{#each foods as food}}
      <li>{{food}}</li>
  {{/each}}
</div>
Enter fullscreen mode Exit fullscreen mode

each/each.dative.js

import Template from './each/each.dative.html'
export let Each = Dative.extend({
   el: "#app",
   data(){
      return {
        foods: ["Rice","Noodles","Vegetables"]
      };
   },
   template: Template
})
Enter fullscreen mode Exit fullscreen mode

You Can Also Catch the Index number of each array

<div>
  {{#each foods as i,food}}
      <li>{{i}}:{{food}}</li>
  {{/each}}
</div>
Enter fullscreen mode Exit fullscreen mode

NOTE When Using the #each block with object the i in the #each block will not be index again we are working on that

Don't Do This You'll Get A Messy Error

<div>
  {{#each foods as food}}
      <li>Type: {{food.type}}</li>
      <li>Name: {{food.name}}</li>
  {{/each}}
</div>
Enter fullscreen mode Exit fullscreen mode

each/each.dative.js

import Template from './each/each.dative.html'
export let Each = Dative.extend({
   el: "#app",
   data(){
      return {
        foods: {
           type: "Fruit",
           name: "Guava"
        }
      };
   },
   template: Template
})
Enter fullscreen mode Exit fullscreen mode

Do This

<div>
  <!-- `i` will give you the key here not the index -->
  {{#each foods as i,food}}
      <li>{{i}}:{{food}}</li>
  {{/each}}
</div>
Enter fullscreen mode Exit fullscreen mode

Thanks For Reading

You Want to Test DativeJs Online

You Can Hit the Follow button on twitter i really appreciate
My Twitter Profile

Thanks Once Again For Reading and Supporting Me

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay