Home Web Front-end HTML Tutorial js template engine handlebars.js practical tutorial??Using index in loop_html/css_WEB-ITnose

js template engine handlebars.js practical tutorial??Using index in loop_html/css_WEB-ITnose

Jun 24, 2016 pm 12:07 PM

 1 <!DOCTYPE html> 2 <html> 3   <head> 4     <META http-equiv=Content-Type content="text/html; charset=utf-8"> 5     <title>关于循环中索引的使用 - by 杨元</title> 6   </head> 7   <body> 8     <h1>关于循环中索引的使</h1> 9     <!--基础html框架-->10     <table>11       <thead>12         <tr>13           <th>序号</th>14           <th>姓名</th>15           <th>性别</th>16           <th>年龄</th>17         </tr>18       </thead>19       <tbody id="tableList">20         21       </tbody>22     </table>23     24     <!--插件引用-->25     <script type="text/javascript" src="script/jquery.js"></script>26     <!--注意!这个例子用的是新版本handlebars,旧版本不支持-->27     <script type="text/javascript" src="script/handlebars-v1.3.0.js"></script>28     29     <!--Handlebars.js模版-->30     <!--Handlebars.js模版放在script标签中,保留了html原有层次结构,模版中要写一些操作语句-->31     <!--id可以用来唯一确定一个模版,type是模版固定的写法-->32     <script id="table-template" type="text/x-handlebars-template">33       {{#each student}}34       <tr>35         <td>{{addOne @index}}</td>36         <td>{{name}}</td>37         <td>{{sex}}</td>38         <td>{{age}}</td>39       </tr>40       {{/each}}41     </script>42     43     <!--进行数据处理、html构造-->44     <script type="text/javascript">45       $(document).ready(function() {46         //模拟的json对象47          var data = {48                       "student": [49                           {50                               "name": "张三",51                               "sex": "0",52                               "age": 1853                           },54                           {55                               "name": "李四",56                               "sex": "0",57                               "age": 2258                           },59                           {60                               "name": "妞妞",61                               "sex": "1",62                               "age": 1963                           }64                       ]65                   };66         67         //注册一个Handlebars模版,通过id找到某一个模版,获取模版的html框架68         //$("#table-template").html()是jquery的语法,不懂的童鞋请恶补。。。69         var myTemplate = Handlebars.compile($("#table-template").html());70         71         //注册一个Handlebars Helper,用来将索引+1,因为默认是从0开始的72         Handlebars.registerHelper("addOne",function(index,options){73           return parseInt(index)+1;74         });75         76         //将json对象用刚刚注册的Handlebars模版封装,得到最终的html,插入到基础table中。77         $('#tableList').html(myTemplate(data));78       });79     </script>80   </body>81 </html>
Copy after login

The sequence number refers to: during the loop process, the index of the current loop. To put it bluntly, it is the current index of the loop. The number of loops usually starts from 0, the same is true in Handlebars.js.

What’s the use? The most common thing is to add a serial number to each row when displaying the table to clearly show how many records there are on the page, as in the example above.

In addition, in order to distinguish different IDs in the loop, Xiaocai just needs a serial number to ensure that the IDs are not repeated. Children's shoes who have used bootstrap should know that many events can be located by ID, such as the Listgroup control.

Without further ado, in Handlebars.js, you can get the current index through {{@index}}, which means that the @index variable represents the current index.

In the above example, it is not good for the table serial number to start from 0, so a Helper is registered and the index is 1.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

See all articles