#6 - template engine: mustache.js

21
Tecniche di animazione digitale 1

Upload: iloveigloo

Post on 15-Jan-2015

1.134 views

Category:

Technology


1 download

DESCRIPTION

- Overview - Mustache.js

TRANSCRIPT

Page 1: #6 - Template Engine: Mustache.js

Tecniche di animazionedigitale 1

Page 2: #6 - Template Engine: Mustache.js

Michele BertoliFounder / Lead Software Engineer at Igloo

[email protected]

Tecniche di animazionedigitale 1

Page 3: #6 - Template Engine: Mustache.js

Client-side 1January 09, 2013

Page 4: #6 - Template Engine: Mustache.js

Agenda

Overview

Mustache.js

5

9

Page 5: #6 - Template Engine: Mustache.js

Overview

Page 6: #6 - Template Engine: Mustache.js

Definition

A template engine is software that is designed to process web

templates and content information to produce output web

documents.

6 Client-side / Overview

Page 7: #6 - Template Engine: Mustache.js

Server side (PHP)

7 Client-side / Overview

<?php while ( have_posts() ) : the_post(); ?>

<article>

<header>

<h1>

<a href="<?php the_permalink(); ?> ">

<?php the_title(); ?>

</a>

</h1>

</header>

<?php the_content(); ?>

</article>

<?php endwhile; ?>

Page 8: #6 - Template Engine: Mustache.js

Server side (.NET)

8 Client-side / Overview

@foreach (var post in Model) {

<article>

<header>

<h1>

<a href="@post.Permalink">

@post.Title

</a>

</h1>

</header>

@post.Content

</article>

}

Page 9: #6 - Template Engine: Mustache.js

Mustache.js

Page 10: #6 - Template Engine: Mustache.js

Definition

Mustache.js is a logic-less template syntax.

It can be used for HTML, config files, source code - anything.

It works by expanding tags in a template using values

provided in a hash or object.

10 Client-side / Mustache.js

http://mustache.github.com/#demo

Page 11: #6 - Template Engine: Mustache.js

Usage

var view = {

name: "World"

};

var template = "Hello {{name}}";

var output = Mustache.render(template, view);

11 Client-side / Mustache.js

http://jsfiddle.net/MicheleBertoli/nwNhd/

Page 12: #6 - Template Engine: Mustache.js

Coffee Break

Page 13: #6 - Template Engine: Mustache.js

Templates

A mustache template is a string that contains any number of

mustache tags.

Tags are indicated by the double mustaches that surround

them.

13 Client-side / Mustache.js

{{name}}

Page 14: #6 - Template Engine: Mustache.js

Sections

Sections render blocks of text one or more times, depending

on the value of the key in the current context.

14 Client-side / Mustache.js

http://jsfiddle.net/MicheleBertoli/nwNhd/1

Page 15: #6 - Template Engine: Mustache.js

Functions

If the value of a section key is a function, it is called with the

section's literal block of text, un-rendered, as its first

argument.

15 Client-side / Mustache.js

http://jsfiddle.net/MicheleBertoli/nwNhd/2

Page 16: #6 - Template Engine: Mustache.js

Inverted sections

The block of an inverted section is rendered only if the value of

that section's tag is null, undefined, false, or an empty list.

16 Client-side / Mustache.js

http://jsfiddle.net/MicheleBertoli/nwNhd/3

Page 17: #6 - Template Engine: Mustache.js

Comments

Comments begin with a bang and are ignored.

17 Client-side / Mustache.js

<h1>Today{{! ignore me }}.</h1>

Page 18: #6 - Template Engine: Mustache.js

Partials

18 Client-side / Mustache.js

http://jsfiddle.net/MicheleBertoli/nwNhd/4

Page 19: #6 - Template Engine: Mustache.js

Compiled templates

Mustache templates can be compiled into JavaScript functions

using Mustache.compile for improved rendering performance.

19 Client-side / Mustache.js

http://jsfiddle.net/MicheleBertoli/nwNhd/5

Page 20: #6 - Template Engine: Mustache.js

Alternatives

● Handlebars● Jade● Underscore

20 Client-side / Mustache.js

http://handlebarsjs.com/

https://github.com/visionmedia/jade

http://documentcloud.github.com/underscore/#template

Page 21: #6 - Template Engine: Mustache.js

Thank you