美文网首页我爱编程
什么是Mustache?前端流行三大件基础

什么是Mustache?前端流行三大件基础

作者: devCK凯 | 来源:发表于2018-06-21 14:50 被阅读18次

Mustache 是一款经典的前端模板引擎,在前后端分离的技术架构中,前端模板引擎是一种可以被考虑的技术选型,随着重型框架(AngularJS、ReactJS、Vue)的流行,前端的模板技术已经成为了某种形式上的标配,Mustache 的价值在于其稳定和经典:
主页:https://github.com/janl/mustache.js/
文档:https://mustache.github.io/mustache.5.html

Mustache 在使用的时候,会在页面上出现 {{person}} 这样的标签,载入的时候回显示出来,然后立即被替换掉,这个对于页面的呈现是不够友好的,这是我在使用的过程中遇到的一个痛点。

Mustache 功能非常经典,这里就能全部罗列出来:

变量

{{person}}

带有HTML的变量

{{{person}}}

循环

{{#persons}}
......
{{/persons}}

数组循环的时候可以用.作为下标
{ "musketeers": ["Athos", "Aramis", "Porthos", "D'Artagnan"] }
{{#musketeers}}
{{.}}
{{/musketeers}}

对象

正常使用:
{ "name": { "first": "Michael", "last": "Jackson" }, "age": "RIP" }
{{name.first}} {{name.last}}
{{age}}

循环使用:
{ "stooges": [ { "name": "Moe" }, { "name": "Larry" }, { "name": "Curly" } ] }
{{#stooges}}
{{name}}
{{/stooges}}

if else

{{#person}}
......
{{/person}}
{{^person}}
......
{{/person}}

布尔判断

和前面循环的语法是一样的,取决于变量是否是一个数组
{{#person}}
......
{{/person}}

数组的布尔判断

当一个数组没有任何值的时候,可能会希望不做任何的显示,所以需要这个判断
{{#persons.length}}
......
{{/persons.length}}

Lambdas

遇到和前面的循环和布尔表达式一样,取决于参数的类型
{{#person}}
{{name}} is awesome.
{{/person}}

{ "name": "Willy", "person": function() { return function(text, render) { return "<b>" + render(text) + "</b>" } } }

输出
<b>Willy is awesome.</b>

注释

这玩意儿有啥用呢?
{{! ignore me }}

Trick

在做<tr></tr>的循环输出的时候,需要使用类似这样的形式(感觉这就是BUG啊,或者是HTML标准的问题?):
``
<tr> <td>{{name}}</td> <td>{{age}}</td> </tr>

两个核心方法

Mustache.parse(template);
Mustache.render(template, obj);

因为动态载入到 HTML 上的事件或者元素会丢失,所以我封装了一个对模板的缓存:

$(templateKey).each(function(i){
    templateExist = false;
    $(templateArray).each(function(index){
        if (templateArray[index][0] == templateKey+i)
        {
            templateExist = true;
            template = templateArray[index][1];
        }
     })

    if (templateExist != true)
    {
        template = $(this).html();
        templateArray.push([templateKey+i, template]);
    }

    Mustache.parse(template);
    $(this).html(Mustache.render(template, item.data)).show();
    if (callbackFunction)
    {
        callbackFunction(item.data);
    };
})


还有Handlebars,这款也非常的知名,并且是基于 Mustache 的模板引擎:
Handlebars:http://handlebarsjs.com/

相关文章

  • 什么是Mustache?前端流行三大件基础

    Mustache 是一款经典的前端模板引擎,在前后端分离的技术架构中,前端模板引擎是一种可以被考虑的技术选型,随着...

  • 【资料】Mustache 语法基础

    1、Mustache 语法基础: Mustache 是一款「logic-less(轻逻辑)」的前端模板引擎,它原本...

  • CSS全面解析之一:HTML基础强化 和 CSS 基础

    第二章:HTML基础强化 前端三大件 HTML 结构 CSS 样式 JavaScript 行为 HTML 常见元素...

  • vue基础入门(3)

    3.组件基础 #3.1.什么是组件? #3.1.1.理解组件 前端组件化开发是目前非常流行的方式,什么是前端组件化...

  • html5

    HTML 前端三大件 HTML 结构 CSS 样式 JavaScript 行为 HTML 常见元素 header ...

  • 1 React基础认知、JSX语法

    React框架基础认知 ->前端三大框架之一 (1).Facebook2013年开源,2015~2016年开始流行...

  • PHP的全栈之路2

    回顾需求 选择前端框架 前端三大件Vue,Angular,React。只听过三位前辈的大名,未曾结识过 起初我打算...

  • 前端基础-什么是前端

    一、 web1.0时代的网页制作 二、 web2.0时代的前端开发 三、 Web前端能做什么? 四、 为什么要学习...

  • 前端templating(Mustache, underscor

    Web 模板引擎是为了使用户界面与业务数据(内容)分离而产生的,Mustache 是一个 logic-less (...

  • 前端基础进阶系列

    前端基础进阶(一):内存空间详细图解前端基础进阶(二):执行上下文详细图解前端基础进阶(三):变量对象详解前端基础...

网友评论

    本文标题:什么是Mustache?前端流行三大件基础

    本文链接:https://www.haomeiwen.com/subject/jinqyftx.html