自行车车把会吧车刮坏吗

by Wing Puah

永帕(Wing Puah)

花10分钟即可开始使用车把 (Take 10 minutes to get started with Handlebars)

Nowadays front-end development is no longer about building static HTML markup and compiling SASS files. The rise of Single Page Applications (SPAs) means we can do a lot of the rendering logic on the client-side. Modern day web development often require dynamic data input.

如今,前端开发不再涉及构建静态HTML标记和编译SASS文件。 单页应用程序(SPA)的兴起意味着我们可以在客户端执行许多呈现逻辑。 现代Web开发通常需要动态数据输入。

While React.js is great, often it requires a learning curve for the developers before they can integrate it into the team. Recently, I was tasked with building the front-end of a course website. That marked the start of my exploration into Handlebars.js.

尽管React.js很棒,但开发人员在将其集成到团队之前通常需要学习曲线。 最近,我受命建立一个课程网站的前端。 这标志着我开始探索Handlebars.js的开始。

Handlebars is a popular and simple templating engine that is simple to use. It looks a lot like regular HTML, with embedded handlebars expressions in the curly braces {{}}.

Handlebars是一种易于使用的流行且简单的模板引擎。 它看起来很像常规HTML,在花括号{{}}中带有嵌入式车把表达式。

<div class="entry">   <h1>{{name}}</h1>   <div>{{quote}}</div> </div>

Before we move on to the details of Handlebars, let’s see how data will be inserted into the page through vanilla Javascript. We will take the example of building a webpage that lists a few quotes. Because, hey, everyone needs some inspiration.

在继续介绍Handlebars的细节之前,让我们看一下如何通过原始Javascript将数据插入页面。 我们将以构建一个列出一些引号的网页为例。 因为,嘿,每个人都需要一些灵感。

香草javascript (Vanilla javascript)

资料检索 (Data retrieval)

Most of the time, you might be retrieving data via ajax, but for simplicity, we will create our own data object.

大多数时候,您可能是通过ajax检索数据,但是为了简单起见,我们将创建自己的数据对象。

// quotes.js var quotes = [   {name: "Frank Lloyd Wright", quote: "You can use an eraser on the drafting table or a sledge hammer on the construction site."},  {name: "Douglas Adams", quote: "The major difference between a thing that might go wrong and a thing that cannot possibly go wrong is that when a thing that cannot possibly go wrong goes wrong it usually turns out to be impossible to get at or repair."},   {name: "Ettore Sottsass", quote: "I try and be as stupid as possible regarding my profession, which means I try to look at as few design magazines as possible."},   {name: "Shaun White", quote: "I’m a big fan of doing what you are really bad at. A lot."} ]

创建HTML标记 (Create HTML markup)

// index.html<div class="container>  <div class="row" id="quotes">  </div></div>

用Javascript添加数据 (Adding the data with Javascript)

We will use a for loop to loop through the content above.

我们将使用for循环遍历上面的内容。

//quotes.jslet quoteMarkup = '';
for (var i = 0; i < quotes.length; i++) {  let name = quotes[i].name,       quote = quotes[i].quote;
quoteMarkup += '<div class="col-12 col-sm-6">' +                  '<h5>' + name + '</h5>' +                  '<p>' + quote + '</p>'                 '</div>'}
document.getElementById('quotes').innerHTML = quoteMarkup;

With code like this, it is difficult to read and tedious to write. And the HTML markup for this page now resides in both the index.html and quotes.js.

使用这样的代码,很难阅读,也很难编写。 现在,此页面HTML标记同时位于index.html和quotes.js中。

输入车把 (Enter handlebars)

入门 (Getting started)

To start off, we need to include the Handlebar source code file. You can add the link inside the head tag or before the end of <body>.

首先,我们需要包含Handlebar源代码文件。 您可以在head标记内或<body>末尾之前添加链接。

&lt;script src="js/handlebars.js">&lt;/script>

Alternatively, you can also link to Handlebars from a CDN.

或者,您也可以从CDN链接到车把。

<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.12/handlebars.min.js"></script>

创建模板 (Create the template)

We will still use the data object of quotes from the file above. We will sprinkle some Handlebars magic on the index.html file.

我们仍将使用上面文件中引号的数据对象。 我们将在index.html文件上添加一些Handlebars魔术。

//index.html<div class="container>  <div id="quotes">
<script id="quotes-template" type="text/x-handlebars-template">                  <div class="row">                    {{#each this}}                      <div class="col-12 col-sm-6 p-3">                          <div class="card">                              <h4 class="card-header">                                  {{name}}                              </h4>                              <div class="card-body">                                  {{quote}}                         </div>                          </div>                     </div>                    {{/each}}                </div>            </script>    </div></div>
  • each: Iterates through the data

    每个 :遍历数据

  • this: References to the current context.

    ,R eferences 在当前情况下。

  • text/x-handlebars-template: To tell the browser not to execute the script as normal Javascript.

    text / x-handlebars-template :告诉浏览器不要像普通Javascript一样执行脚本。

使用把手编译模板 (Compile the template with Handlebars)

It only takes a few lines to compile the data with Handlebars. That is what I love about it. Even if someone on the team has not used Handlebars before, the script and markup are very simple for them to understand and pick up.

只需几行即可使用Handlebars编译数据。 那就是我所喜欢的。 即使团队中的某人以前从未使用过Handlebars,脚本和标记也很容易让他们理解和掌握。

let content = document.getElementById('quotes'),    src = document.getElementById('quotes-template').innerHTML,     template = Handlebars.compile(src),            html = template(quotes);
content.innerHTML = html;
  • content: Returns the element into which you want to insert the compiled information.

    content :返回要在其中插入编译信息的元素。

  • src: Retrieves the markup of the template.

    src :检索模板的标记。

  • Handlebars.compile(src): Compiles the template in use. It will return a function that the data can be passed to so it can be be rendered.

    Handlebars.compile(src) :编译正在使用的模板。 它将返回一个函数,数据可以传递到该函数,以便可以呈现它。

  • template(quotes): Compiles the data into the template.

    template(quotes) :将数据编译到模板中。

  • content.innerHTML: Renders the above to the DOM

    content.innerHTML :将以上内容呈现给DOM

You can view the project here.

您可以在此处查看项目 。

奖金 (Bonus)

I used Handlebars for a multiple-templates website. I found myself writing the same ajax and Handlebars code multiple times. So, here are the two functions that I created to make my life easier.

我将Handlebars用于多个模板的网站。 我发现自己多次编写相同的ajax和Handlebars代码。 因此,这是我创建的两个使我的生活更轻松的函数。

使用Javascript从Ajax获取数据 (Getting data from ajax with Javascript)

function ajaxGet(url, callback) {    let xmlhttp = new XMLHttpRequest();    xmlhttp.onreadystatechange = function() {        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {            // console.log(xmlhttp.responseText);            try {                var data = JSON.parse(xmlhttp.responseText);            } catch(err) {                console.log(err.message +' Getting: ' + url);                return;            }            callback(data);        }    };
xmlhttp.open("GET", url, true);    xmlhttp.send();}

运行车把的功能 (Function to run Handlebars)

function runHandlebars(id, dataSrc, src) {  if(document.getElementById(id) != null) {    let content = document.getElementById(id);    ajaxGet(dataSrc, function(data){      let source = document.getElementById(src).innerHTML,           template = Handlebars.compile(source);
content.innerHTML = template(data);    });  }}

With these two functions, I could run all my Handlebars code on a single Javascript file. It will look something like this.

有了这两个功能,我可以在一个Javascript文件中运行所有的Handlebars代码。 它看起来像这样。

runHandlebars('nav-sub-1', '/data/courses.json', 'nav-submenu-template');
runHandlebars('contributors', '/data/contributors.json', 'contributors-template');

结论 (Conclusion)

My experience with Handlebars has been a positive one. In my project, I use it with gulp and metalsmith. Will I use it for other projects? My take is I prefer something like React or a full fledged static site generator like Jekyll. But in this case, when the team is more comfortable with HTML markup and it is a relatively simple website, Handlebars is a good choice.

我在车把方面的经验很积极。 在我的项目中,我将它与gulp和metalsmith一起使用。 我将其用于其他项目吗? 我的看法是,我更喜欢像React这样的东西或像Jekyll这样的成熟的静态站点生成器。 但是在这种情况下,当团队更熟悉HTML标记并且它是一个相对简单的网站时,Handlebars是一个不错的选择。

翻译自: https://www.freecodecamp.org/news/take-10-minutes-to-get-started-with-handlebars-298632ed82ab/

自行车车把会吧车刮坏吗

自行车车把会吧车刮坏吗_花10分钟即可开始使用车把相关推荐

  1. uci拒绝认证_关于车架上UCI认证贴纸的10个常见问题

    关于车架上UCI认证贴纸的10个常见问题 作者: 单车时代 2019-05-10 16:34:33 4266 车架上面为何会有UCI贴纸?有这张贴纸跟没这张贴纸又有何差异? 1.UCI认证的由来 为了 ...

  2. 向银行贷款20万, 分期三年买50万的车,个人借款40万, 贷款10年买200万的房子,再贷款120万分创业...

    向银行贷款20万 按1年期贷款利率为:6%,若按年还贷款,银行贷款利息为:200,000*6%=12,000. 连本带息:20*106%=21.2万 分期三年买50万的车 贷款总额30万 年利率按10 ...

  3. 7号电单车java怎么样_7号电单车“方便”变不方便,定点还车坑坏网友!

    2017-08-22/17:51 驱动中国2017年8月22日消息    在ofo.摩拜等共享单车企业火了之后,国内共享市场领域也 随之火了起来,很快便出现共享汽车.共享充电宝.共享雨伞等等接踵而至. ...

  4. ssm的校园单车自行车租赁系统|租车系统计算机专业毕业论文java毕业设计开题报告

  5. 计算机毕业论文java毕业设计选题源代码ssm的校园单车自行车租赁系统|租车系统

  6. java公路车几何有问题_教你读懂公路车架几何

    文:Lurker  编辑:Simon 随着国内公路车运动的逐渐升温,很多车友慢慢意识到挑选一台公路车除了看配置和传统上的尺寸之外,也注意到需要关心这款车架的几何尺寸.而对一些相对专业.学习面广的车店来 ...

  7. 如何判断笔记本蓝牙硬件坏了_还在担心被套路?老司机教你如何判断车用尿素溶液的好与坏...

    车用尿素液是指浓度为32.5±0.7%(国标尿素浓度)的高纯尿素且溶剂为去离子超纯水的液体,在SCR催化反应罐中通过氧化还原反应,将有毒物质氮氧化物生成无污染的氮气和水排出,从而达到节能.减排的目的. ...

  8. 智能车的转弯部分_邛崃斯维刻平衡车维修平衡车不充电怎么处理哪里有

    邛崃斯维刻平衡车维修平衡车不充电怎么处理哪里有 智能平衡车的用途 如今的道路上,早已不仅是汽车.自行车.电动车等传统交通工具全制霸的局面了,车流与车流中间,也不时夹在着智能滑板车.电单车.平衡车等的身 ...

  9. 第十六届全国大学生智能车提问与回复 |7月10日

    简 介: 本文汇集了参加第十六届全国大学生智能车竞赛参赛队伍的提问与相应的回复. 关键词: 智能车竞赛,电机,轮胎 01 烧毁电机 ■ 提问: 卓大大,我们四轮3个月烧了6个电机,今晚烧了两个,跑了3 ...

最新文章

  1. android 释放 so,这 10 个值得开启的隐藏功能,让你的 Chrome 释放更多潜力
  2. 青龙羊毛——小虎饿了(偷的)
  3. 【机器学习实战】第1章 机器学习基础
  4. 为什么我不再使用MVC框架
  5. 线性回归(y=ax+b)
  6. sklearn自学指南(part5)--使用手册的目录
  7. php删除第一个字母,php – 正在上传的文件将第一个字母切断
  8. 设计模式之Strategy(策略模式系列1)
  9. nginx ngx_http_auth_basic_module(Basic Authentication)
  10. 2010工作代码总结之三(repostioryItemGridLookUpEdit下拉框)
  11. Intel 平台编程总结----SIMD技术
  12. .NET性能系列文章二:Newtonsoft.Json vs System.Text.Json
  13. 中级职称的计算机考试题库,中级职称计算机考试模拟题库及答案
  14. [阿毛]Ubuntu 16安装CH340串口驱动
  15. C#,入门教程——列表(List)的基础知识
  16. android : 小米手机 打开开发者 选项 PC 端 安装 apk
  17. win10家庭版下面修改配置host文件虚拟主机
  18. 腾讯搜搜的分类搜索代码
  19. 王者荣耀测试自己本命英雄软件,王者荣耀中谁是你的本命英雄测试地址 趣推测试王者荣耀中谁是你的本命英雄...
  20. 陆奇最新演讲:如何成为一个优秀的工程师

热门文章

  1. JavaWeb项目第三次总结_成绩查询的实现
  2. python中append的用法_Python 列表 append() 使用方法及示例
  3. iOS 关于手机权限的检查与获取
  4. WebViewJavascriptBridge的简单使用
  5. 仅需6步,教你轻易撕掉app开发框架的神秘面纱(1):确定框架方案
  6. uniapph5配置index.html模板路径不生效解决办法
  7. ES6 常用的特性整理
  8. 6 OC 中的isa 指针
  9. 第五课:系统目录及ls·文件类型及alias命令介绍
  10. bzoj1688[Usaco2005 Open]Disease Manangement 疾病管理*