• Stars
    star
    336
  • Rank 125,564 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The fastest and smallest Mustache compliant Javascript templating library written in 2167 bytes (uncompressed)

templayed.js

The fastest and smallest Mustache compliant Javascript templating library written in 2167 bytes (uncompressed)

Introduction

Mustache.js is one of the best known Javascript templating libraries out there. I am also using it within several projects. The only downside you could imagine is its size:

  • uncompressed: 15050 bytes
  • YUI compressed: 5053 bytes
  • YUI compressed and gzipped: 2271 bytes

So out of pure curiosity, I have written templayed.js to see whether it can be written more compact with the following as result:

  • uncompressed: 2167 bytes
  • YUI compressed: 1324 bytes
  • YUI compressed and gzipped: 655 bytes

It does not have dependencies and it supports the following Mustache.js features:

  • variables
  • variables accessed through properties of objects using the dot notation
  • HTML escaping of variables
  • shown / hidden sections
  • lists
  • current item referrals
  • functions
  • inverted sections
  • comments

This leaves out partials and streaming.

Benchmarks with Mustache.js, Handlebars.js and Hogan.js

Handlebars.js is also a well-known Mustache compliant Javascript templating library (uncompressed 49699 bytes) written by Yehuda Katz (@wycats) and it is a bit more extensive than Mustache.js. Hogan.js is the Mustache templating library (uncompressed 15187 bytes) developed by Twitter.

As of version 0.2.0, templayed.js compiles templates to cached functions for major performance improvements. I have benchmarked Mustache.js, Handlebars.js, Hogan.js and templayed.js.

You can view the results at Mustache JS engine rumble: Mustache.js vs Handlebars.js vs Hogan.js vs templayed.js in which templayed.js is pointed out as the fastest library in ALL browsers!

Open the benchmark link so you can run the benchmark in your browser yourself.

JSPerf benchmark results

Testing templayed.js with QUnit

The templayed.js library is tested with QUnit. Check out the test results at http://archan937.github.com/templayed.js/test.

Installation

Just include templayed.js:

<script src="path/to/templayed.js" type="text/javascript"></script>

Note: include templayed.min.js for the minified templayed.js library

Usage

Writing templates

If you are familiar with the Mustache.js syntax then you are able to write templayed.js templates. Read the Mustache.js documentation if you are not.

Variable scoping

As of version 0.2.0, the variable scope handling is altered. You have to use ../ which will evaluate the path against a parent context:

template  = "<ul>{{#names}}<li>{{../fullName}}</li>{{/names}}</ul>",
variables = {
  names: [{firstName: "Paul", lastName: "Engel"}, {firstName: "Chunk", lastName: "Norris"}],
  fullName: function() {
    return this.lastName + ", " + this.firstName;
  }
};
templayed(template)(variables); //=> "<ul><li>Engel, Paul</li><li>Norris, Chunk</li></ul>";

Rendering templates

Rendering those templates can be done using templayed().

templayed("<p>My name is {{name}}!</p>")({name: "Paul Engel"});

Check current version

You can obtain the library version with the following:

templayed.version;

Using templayed.js in node.js

Helder Santana has ported templayed.js for node.js. Install templayed.js for node.js as follows:

npm install templayed

See npmjs/package/templayed for more information.

Handlebars.js-ish helpers

The edge branch contains a templayed.js version in which Handlebars.js-ish helpers are implemented.

Try out templayed.js online yourself

Please visit http://archan937.github.com/templayed.js to try out templayed.js yourself and to check out a series of live examples.

TODO

  • Automatically cache compiled template functions
  • Support custom delimiters (e.g. <% %>)

Contact me

For support, remarks and requests, please mail me at [email protected].

License

Copyright (c) 2023 Paul Engel, released under the MIT license

http://github.com/archan937 - http://twitter.com/archan937 - [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

csonv.js

A tiny library to fetch relational CSV data at client-side just like JSON
JavaScript
271
star
2

ruby-mass

Introspect the Ruby Heap by indexing, counting, locating references to and detaching (in order to release) objects - optionally narrowing by namespace
Ruby
246
star
3

jsonv.sh

A Bash command line tool for converting JSON to CSV
Awk
216
star
4

topup

The #1 Javascript Pop Up / Lightbox made by Paul Engel
JavaScript
159
star
5

motion-bundler

Use Ruby gems and mock require statements within RubyMotion applications
Ruby
98
star
6

clickhouse

A Ruby database driver for Clickhouse
JavaScript
89
star
7

stack_tracy

Investigate and detect slow methods within the stack trace of your Ruby (optionally Sinatra) application
Ruby
77
star
8

mecks_unit

A simple Elixir package to elegantly mock module functions within (asynchronous) ExUnit tests using Erlang's :meck library
Elixir
55
star
9

ex_united

Easily spawn Elixir nodes (supervising, Mix configured, easy asserted / refuted) within ExUnit tests
Elixir
43
star
10

rich_cms

Enrichments (e9s) module for a pluggable CMS frontend
Ruby
42
star
11

e9s

Enrichments (e9s) for a pluggable CMS, internationalization (i18n) and localized pluralization
Ruby
40
star
12

jazz_libs

A small gem for rolling out JS libraries (includes repository, demo page and version release rake task)
Ruby
38
star
13

slot_machine

Ruby gem for matching available slots (time slots are also supported)
Ruby
24
star
14

lock-o-motion

Require and mock Ruby gems (including their dependencies) within RubyMotion applications
Ruby
23
star
15

dirty_hashy

Dirty tracking within hashes (with or without indifferent access) or objects as it is expected to be!
Ruby
15
star
16

animate.js

Add slick animations to your web pages and page transitions
JavaScript
14
star
17

raccoon_tip

A lightweight jQuery based balloon tip library
JavaScript
9
star
18

jzip

A Rails gem for Javascript merging and compression using templates (like SASS)
JavaScript
9
star
19

e9s-demo

Rails 3 (and also Rails 2) demo application of the Enrichments (e9s) gem
JavaScript
9
star
20

rich_pluralization

Enrichments (e9s) module for localized pluralization
Ruby
9
star
21

rich_i18n

Enrichments (e9s) module for i18n
Ruby
9
star
22

designer.js

A minimalistic Javascript library to design web pages using absolute positioning
JavaScript
8
star
23

bugs_bunny

A Unicorn served Sinatra demo app which uses Bunny to broadcast AMQP messages to every worker
Ruby
7
star
24

monetdb

A pure Ruby database driver for MonetDB (monetdb5-sql)
Ruby
6
star
25

cached_record

Cache (and optionally memoize) ActiveRecord or DataMapper records in Redis or Memcached
Ruby
5
star
26

seat_holder

The modest Javascript placeholder
JavaScript
4
star
27

unextendable

A small gem making unextending extended module methods within object instances possible
Ruby
4
star
28

relexer.js

A very simple Javascript lexer and parser
JavaScript
4
star
29

oned.js

Trigger callback functions when native HTML or jQuery elements get added to the DOM tree
JavaScript
3
star
30

webhead

An easy-to-use Node web crawler storing cookies, following redirects, traversing pages and submitting forms.
JavaScript
3
star
31

vps

Zero-config deployments of Plug, Phoenix, Rack and Rails apps on a clean Ubuntu server using Docker and Let's Encrypt
Ruby
2
star
32

directiveadmin

A layer on top of ActiveAdmin for adding more power and flexibility (opinionated customizations)
Ruby
2
star
33

rich_support

A small gem making your own gem Rails 2 and 3 compliant and providing the String class a few goodies
Ruby
2
star
34

portcat

Alpine based Docker container with socat installed that can easily map multiple ports to another container
Shell
2
star
35

el.js

A straightforward and lightweight Javascript library for data-binded template rendering
JavaScript
2
star
36

google-apis

A thin layer on top of Google::APIClient for a more intuitive way of working (e.g. with BigQuery or Cloud Storage)
Ruby
2
star
37

directiverecord

A layer on top of ActiveRecord for using paths within queries without thinking about association includes
Ruby
2
star
38

iRacingHUD

Create your own reactive Javascript based UI iRacing HUDs
JavaScript
2
star
39

clustorage

Elixir cluster to store and distribute data and functions by code compilation and hot loading done by a designated "loader node"
Elixir
2
star
40

new_class

Define variable dependent classes without evalling
Ruby
1
star
41

dollar.js

A minimalistic Javascript library for DOM manipulation, template rendering (data binded), event binding, introspection
JavaScript
1
star
42

gem_suit

Test the entire usage workflow (including generators) of your newly generated or existing gem within Rails 2 and 3.
Ruby
1
star