AlexBrand
AlexBrand

Reputation: 12399

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.

Upvotes: 2

Views: 1632

Answers (4)

daamsie
daamsie

Reputation: 1598

One of the main attractions of Mustache to me is that there are so many implementations of it.

As a simple example, you might be creating a list of products.

  1. Server side - Load up the first 25 products. Render the products with the server side implementation of 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.

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.

Upvotes: 0

Alexis C.
Alexis C.

Reputation: 4918

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.

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.

Upvotes: 0

swiecki
swiecki

Reputation: 3483

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. The way this is different from PHP is that there are no if statements, else clauses, or for loops. 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.

More information/source: http://mustache.github.com/mustache.5.html

Upvotes: 2

Dave Newton
Dave Newton

Reputation: 160181

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.

Upvotes: 4

Related Questions