mustache

Mustache 入门教程

匿名 (未验证) 提交于 2019-12-02 23:59:01
Mustache 是一个轻逻辑模板解析引擎,它的优势在于可以应用在 Javascript、PHP、Python、Perl 等多种编程语言中。 Mustache 的模板语法很简单,就那么几个: {{keyName}} {{#keyName}} {{/keyName}} {{^keyName}} {{/keyName}} {{.}} {{<partials}} {{{keyName}}} {{!comments}} 传统的写后台ajax过来的数据我们会 : $ . each ( messages . reverse (), function ( index , message ) { $ ( '#messageList' ). append ( '<li><span class="list-title">' + message . userName + '</span>' + '<abbr class="list-timestamp" title="' + message . datePosted + '"></abbr>' + '<p class="list-text">' + message . messageText + '</p></li>' ); } }); 不停的进行字符串的拼接,那么使用mustache后,我们可以 : <div id = "wrap2" >

Javascript模版引擎mustache.js简介

匿名 (未验证) 提交于 2019-12-02 21:53:52
最近使用ELK的sentinl进行告警配置,sentinl的邮件通知支持mustache,借此机会学习了mustache相关知识,记录在此。 mustache的核心是标签和logic-less. 标签: 定义模板的时候,使用了{{name}}、{{#systems}}{{/systems}}标记. 这就是mustache的标签,只不过用{{}}替代了<>,以免和html标签的<>混淆. Logic-less:轻逻辑,因为在定义模板的时候不会使用if-else,不会有循环式编码,一切都用标签来解决. 标签包含: {{prop}}: 将数据源对象上prop属性对应的值,转换成字符串输出. {{{prop}}} ――不进行特殊字符转义,保持内容原样输出 {{#prod}}{{/prop}} {{^prop}}{{/prop}} {{.}} ――表示枚举,可以循环输出整个数组 {{>partials}} ――以>开始表示子模块 {{!comments}} ――注释 mustache渲染{{prop}}标签的逻辑: 1)如果prop引用的值上null或者undefined,则渲染成空串; 2)如果prop引用的是一个函数,则在渲染的时候自动执行这个函数,并把函数返回值作为渲染结果. 3)其他场景,直接把prop引用的值转换成字符串作为渲染结果. 默认情况下,mustache在渲染prop时

Django and Mustache use the same syntax for template

点点圈 提交于 2019-12-02 20:25:00
I try to smuggle HTML template in the HTML for mustache.js, however the django template engine remove all the placeholders that should be output as-is to the front-end The template is included in HTML in this way: <script type="text/x-mustache-template" data-id="header_user_info"> <div id="header_user_info"> <div id="notification">0</div> <a href="#">{{username}}</a> </div> </script> and I can get the HTML template by running $(el).html(), and generate html by using Mustache.to_html(temp, data); I could put all the template into another static file and serve from CDN, but then it would be hard

mustache-前端html模板渲染工具

强颜欢笑 提交于 2019-12-02 19:25:35
为什么需要渲染工具 为了理解该问题,我们先看几个示例: //示例1 var b = { Id: "123", Name: "张三", Age: 14 } var output = "学生学号:" + b.Id + " 学生名称:" + b.Name + " 学生年龄:" + b.Age //示例2 var list = [{ Id: "123", Name: "张三", Age: 14, Class: "", SubChild: [{ Id: "123", Name: "张三", Age: 14 }] }] var html = ""; for (var i = 0; i < list.length; i++) { html = html + "<div class='" + list[i].Class + "' id='" + list[i].Id + "'>" + list[i].Name if (list[i].SubChild) { html += "<div>" for (var j = 0; j < list[i].SubChild; j++) { html += "<span>" + list[i].SubChild[j].Name + "</span>" } html += "</div>" } html += "</div>" } 对示例1,我们经常遇到

Mustache.js + jQuery: what is the minimal working example ?

允我心安 提交于 2019-12-02 19:00:48
I would like to use mustache.js with jQuery in my HTML5 app, but I can't make all the component work together. Every file is found, there is no problem here (the template is loaded roght, I can see its value in the Firebug debugguer). Here is my index.html : <!DOCTYPE html> <html lang="fr"> <head><meta charset="utf-8"></head> <body> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> <script src="../js/jquery.mustache.js"></script> <script src="../js/app.js"></script> <div id="content"></div> </body> </html> Here is my app.js file : $(document).ready(function(

How to use Backbone.Marionette.ItemView with Mustache

落爺英雄遲暮 提交于 2019-12-02 18:36:42
The following code works fine using Backbone.Marionette.ItemView but not Mustache . Backbone.Marionette.ItemView - no Mustache I would like to use the same code but loading the template varaible using Mustache . Here is my code: Backbone.Marionette.ItemView - with Mustache Any idea why my code does not work and why? Thanks Marionette assumes the use of UnderscoreJS templates by default. Simply replacing the template configuration for a view isn't enough. You also need to replace how the rendering process works. In your simple example, you only need to override the Marionette.Renderer.render

How to decode HTML entity with Handlebars

不羁岁月 提交于 2019-12-02 18:13:51
I'm using the Handlebars templating engine on the app I'm building to render the data I get from the server. I know that it escapes HTML values by default and that you have to use the triple brackets {{{text}}} in order for text: <p>Example</p> to be rendered as an HTML element. The problem is, what do I do if the data I receive, including the HTML tags, is already escaped? So, if I receive data like: text: <p>Example</p> How do I force handlebars to translate it and render it as normal HTML? You have to decode it first, then pass it to handlebars with triple brackets. I know a small tip to

What's the least redundant way to make a site with JavaScript-generated HTML crawlable?

送分小仙女□ 提交于 2019-12-02 16:45:41
After reading Google's policy on making Ajax-generated content crawlable , along with many developers' blog posts and Stackoverflow Q&A threads on the subject, I'm left with the conclusion that there is no way to make a site with only JavaScript/Ajax-generated HTML crawlable. A site I'm currently working isn't getting a fair amount of its content indexed. All of the presentation layer for our non-indexed content is built in JavaScript by generating HTML from JSON returned from Ajax-based web service calls, and we believe Google is not indexing the content because of that. Is that correct? The

Can Mustache Templates do template extension?

为君一笑 提交于 2019-12-02 16:35:40
I'm new to Mustache. Many templating languages (e.g., Django / Jinja ) will let you extend a "parent" template like so... base.html <html><head></head> <body> {% block content %}{% endblock %} </body> </html> frontpage.html {% extends "base.html" %} {% block content %}<h1>Foobar!</h1>{% endblock %} rendered frontpage.html <html><head></head> <body> <h1>Foobar!</h1> </body> </html> I'm aware of Mustache's partials (e.g., {{>content}} ), but those seem to be just includes . Does template extension exist for Mustache? Or, failing that, is there at least some design pattern that effectively turns

Difference between react.js and Ajax

南笙酒味 提交于 2019-12-02 15:55:16
When I googled about React.js what I got is: React.js is a Framework that is used to create user interfaces. If a particular part of the website is frequently updated that means we can use react. But I am confused that Ajax has been used for this only. We can update a part of site using Ajax without page refresh. For templating we would be using handlebars and mustache. Could somebody explain me in what ways react is different from Ajax and why we should use it. Ajax is used to refresh a web page without having to reload it : it sends a request to the server, but typically the response is