简体   繁体   English

小胡子模板库?

[英]Mustache template library?

What are some use cases for mustache? 小胡子有哪些用例?

I just discovered it today but I can't seem to understand how is it different from just creating regular template files in your framework (cakePHP, django, etc..) or just having an html + php file. 我今天才发现它,但我似乎不明白它与在您的框架中创建常规模板文件(cakePHP,django等)或仅包含html + php文件有何不同。

Mustache allows almost no intelligence in the view--separation of concerns is its usecase. 小胡子在视图中几乎没有智慧-关注点分离是其用例。

It is another template engine/library, the only (real) difference is in its syntax and philosophy. 它是另一个模板引擎/库,唯一(真正的)区别在于其语法和理念。

The whole point of Mustache is that it's logic-less. 小胡子的全部要点是它没有逻辑。 You pass it well-formatted JSON, and it does the rest in a super-simple syntax. 您传递格式正确的JSON,其余的将以超简单的语法完成。 The way this is different from PHP is that there are no if statements, else clauses, or for loops. 与PHP的不同之处在于,没有if语句,else子句或for循环。 Instead there are only tags. 相反,只有标签。 Some tags are replaced with a value, some nothing, and others a series of values. 一些标签被替换为一个值,有些则被替换为空,而另一些则被替换为一系列值。 You don't have multiple arrays that you have to manage, just one javascript object that you set and forget and watch the page render. 您不必管理多个数组,而只需设置并忘记一个javascript对象并观看页面渲染即可。

More information/source: http://mustache.github.com/mustache.5.html 更多信息/来源: http : //mustache.github.com/mustache.5.html

Previous responses omit the fact that with a library as Mustache the entire page rendering is done client-side , whereas most template engine are aften used to render partials and formatting server-side. 先前的响应忽略了一个事实,即以Mustache为库, 整个页面呈现都在客户端完成 ,而大多数模板引擎通常用于呈现部分内容和格式化服务器端。

The main use case I see for this library is to create web-apps based on JSON or XML webservices served from a server you don't have access to. 我在该库中看到的主要用例是,基于无法访问的服务器提供的JSON或XML Web服务创建Web应用。

One of the main attractions of Mustache to me is that there are so many implementations of it. 对我而言,Mustache的主要吸引力之一是它的实现方式太多。

As a simple example, you might be creating a list of products. 作为一个简单的示例,您可能正在创建产品列表。

  1. Server side - Load up the first 25 products. 服务器端-加载前25个产品。 Render the products with the server side implementation of Mustache. 使用Mustache的服务器端实现来渲染产品。
  2. Client side - User scrolls down the page and clicks load more. 客户端-用户向下滚动页面,然后单击以加载更多内容。 At this point you fetch the other products with an Ajax call and render them with the JS implementation of Mustache. 此时,您将通过Ajax调用获取其他产品,并使用Mustache的JS实现呈现它们。

Same template, reused both client-side and server-side. 相同的模板,在客户端和服务器端均可重用。 If you ever need to change the HTML you are only doing it in one place and it will be consistent with both server-side rendered and client-side rendered content. 如果您需要更改HTML,则只需在一个地方进行操作,即可与服务器端呈现的内容和客户端呈现的内容保持一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM