- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章JsRender实用入门教程由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文是一篇JsRender的实用入门教程,实例讲述了tag else使用、循环嵌套访问父级数据等知识点。分享给大家供大家参考。具体如下:
前言 。
JsRender是一款基于jQuery的JavaScript模版引擎,它具有如下特点:
· 简单直观 。
· 功能强大 。
· 可扩展的 。
· 快如闪电 。
这些特性看起来很厉害,但几乎每个模版引擎,都会这么宣传。。.
由于工作需要,小菜才接触到此款模版引擎。使用了一段时间,发现它确实比较强大,但小菜觉得有些地方强大的过头了,反倒让人觉得很难理解.
另一方面,JsRender的官方文档比较详细,但其他资料出奇的少,遇到点什么问题,基本搜不到,不仅仅是相关问题搜不到,几乎就是没有结果.
再加上JsRender有些地方确实是不好理解,所以急需小菜分享一些“最佳实践”.
基于最近一段时间的使用,小菜总结了一些实用经验,当然,这些经验在官方文档上是找不到的.
嵌套循环使用#parent访问父级数据(不推荐) 。
。
。
嵌套循环使用参数访问父级数据(推荐) 。
。
。
自定义标签(custom tag)中使用else(强烈不推荐) 。
。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>自定义标签中使用else --- by 杨元</title> <style> </style> </head> <body> <div> <table> <thead> <tr> <th width="50%">名称</th> <th width="50%">单价</th> </tr> </thead> <tbody id="list"> </tbody> </table> </div> <script src="jquery.min.js"></script> <script src="jsviews.js"></script> <!-- 定义JsRender模版 --> <script id="testTmpl" type="text/x-jsrender"> <tr> <td>{{:name}}</td> <td> {{!-- isShow为自定义标签,price是传入的参数,status是附加属性 --}} {{isShow price status=0}} {{:price}} {{else price status=1}} -- {{/isShow}} </td> </tr> </script> <script> //数据源 var dataSrouce = [{ name: "苹果", price: 108 },{ name: "鸭梨", price: 370 },{ name: "桃子", price: 99 },{ name: "菠萝", price: 371 },{ name: "橘子", price: 153 }]; //自定义标签 $.views.tags({ "isShow": function(price){ var temp=price+''.split(''); if(this.tagCtx.props.status === 0){ //判断价格是否为水仙花数,如果是,则显示,否则不显示 if(price==(Math.pow(parseInt(temp[0],10),3)+Math.pow(parseInt(temp[1],10),3)+Math.pow(parseInt(temp[2],10),3))){ return this.tagCtxs[0].render(); }else{ return this.tagCtxs[1].render(); } }else{ return ""; } } }); 。
。
//渲染数据 var html = $("#testTmpl").render(dataSrouce); $("#list").append(html); </script> </body> </html> 。
。
用helper代替自定义标签(推荐) 。
。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>用helper代替自定义标签 --- by 杨元</title> <style> </style> </head> <body> <div> <table> <thead> <tr> <th width="50%">名称</th> <th width="50%">单价</th> </tr> </thead> <tbody id="list"> </tbody> </table> </div> <script src="jquery.min.js"></script> <script src="jsviews.js"></script> <!-- 定义JsRender模版 --> <script id="testTmpl" type="text/x-jsrender"> <tr> <td>{{:name}}</td> <td> {{!-- 利用原生的if做分支跳转,利用helper做逻辑处理 --}} {{if ~isShow(price)}} {{:price}} {{else}} -- {{/if}} </td> </tr> </script> <script> //数据源 var dataSrouce = [{ name: "苹果", price: 108 },{ name: "鸭梨", price: 370 },{ name: "桃子", price: 99 },{ name: "菠萝", price: 371 },{ name: "橘子", price: 153 }]; //Helper $.views.helpers({ "isShow": function(price){ var temp=price+''.split(''); if(price==(Math.pow(parseInt(temp[0],10),3)+Math.pow(parseInt(temp[1],10),3)+Math.pow(parseInt(temp[2],10),3))){ return true; }else{ return false; } } }),
。
//渲染数据 var html = $("#testTmpl").render(dataSrouce); $("#list").append(html); </script> </body> </html> 。
。
完整实例代码点击此处本站下载.
希望本文所述对大家JsRender程序设计的学习有所帮助.
最后此篇关于JsRender实用入门教程的文章就讲到这里了,如果你想了解更多关于JsRender实用入门教程的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
Hive —— 入门 Hive介绍 Apache Hive是一款建立在Hadoop之上的开源数据仓库系统,可以将存储在Hadoop文件中的结构化、半结构化数据文件映射为一张数据库表,基于表提供了一
HBase —— 入门 HBase介绍 HBase是一个分布式的、面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”
零:前端目前形势 前端的发展史 HTML(5)、CSS(3)、JavaScript(ES5、ES6):编写一个个的页面 -> 给后端(PHP、Python、Go、Java) ->
在本教程中,您将了解在计算机上运行 JavaScript 的不同方法。 JavaScript 是一种流行的编程语言,具有广泛的应用程序。 JavaScript 以前主要用于使网页具有交
我曾经是一个对编程一窍不通的小白,但因为对互联网世界的好奇心和求知欲的驱使,我踏入了编程的殿堂。在学习的过程中,我发现了一门神奇的编程语言——Python。Python有着简洁、易读的语法,让初学者能
嗨,亲爱的读者们! 今天我要给大家分享一些关于Python爬虫的小案例。你是否曾为了获取特定网页上的数据而烦恼过?或者是否好奇如何从网页中提取信息以供自己使用?那么,这篇文章将会给你一些启示和灵感。
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 8 年前。 Improv
我想创建一个像https://apprtc.appspot.com/?r=04188292这样的应用程序。我对 webrtc 了解一点,但无法掌握 google app-engine。如何为 java
我刚刚开始使用 Python 并编写了一个简单的周边程序。但是,每当我在终端中键入 python perimeter.py 时,都会收到以下错误,我不知道如何解决。 >>> python perime
Redis有5个基本数据结构,string、list、hash、set和zset。它们是日常开发中使用频率非常高应用最为广泛的数据结构,把这5个数据结构都吃透了,你就掌握了Redis应用知识的一半了
创建发布web项目 具体步骤: 1.在开发工具中创建一个dynamic web project helloword 2.在webContent中创建index.html文件 3.发布web应用到
如果你在 Ubuntu 上使用终端的时间很长,你可能会希望调整终端的字体和大小以获取一种良好的体验。 更改字体是一种最简单但最直观的 Linux 的终端自定义 的方法。让我
1. 前言 ADODB 是 Active Data Objects Data Base 的简称,它是一种 PHP 存取数据库的函式组件。现在 SFS3 系统 (校园自由软件交流网学务系统) 计划的
我对 neo4j 完全陌生,我很抱歉提出这样一个基本问题。我已经安装了neo4j,我正在使用shell“localhost:7474/webadmin/#/console/” 我正在寻找一个很好的例子
我正在阅读 ios 4 的核心音频,目的是构建一个小测试应用程序。 在这一点上,我对所有 api 的研究感到非常困惑。理想情况下,我想知道如何从两个 mp3 中提取一些样本到数组中。 然后在回调循环中
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是无关紧要的,因
我下载了 GNUStep并安装了它,但是我不确定在哪里可以找到 IDE。有谁知道什么程序可以用作 GNUStep IDE/从哪里获取它们?否则,有没有人知道有关如何创建和编译基本 GNUStep 程序
我正在尝试开始使用 Apache Solr,但有些事情我不清楚。通读tutorial ,我已经设置了一个正在运行的 Solr 实例。我感到困惑的是 Solr 的所有配置(架构等)都是 XML 格式的。
请问有没有关于如何开始使用 BruTile 的文档? 我目前正在使用 SharpMap,我需要预缓存切片以加快进程 最佳答案 我今天正在研究这个:)Mapsui项目site严重依赖 SharpMap
尽我所能,我无法让 CEDET 做任何事情。 Emacs 24.3。我下载了最新的 CEDET 快照。我从他的底部(不是这样)Gentle Introduction 中获取了 Alex Ott 的设置
我是一名优秀的程序员,十分优秀!