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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

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.

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.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

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.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

See all articles