
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
micromustache
Advanced tools
A stripped down version of the {{mustache}} template engine with JavaScript
#MicroMustache
A stripped down version of the {{mustache}} template engine with JavaScript. It covers the most important use case for Mustache: replacing variables with their names. MicroMustache doesn't support partials, array and nested objects. It is about 40% faster than Mustache.js and 93% smaller! If all you do is variable name replacement, you can easily cut that overhead.
[Download Development Edition] (https://raw.github.com/hanifbbz/micromustache/master/dist/micromustache.js) - 2,65 KB
[Download Production Edition] (https://raw.github.com/hanifbbz/micromustache/master/dist/micromustache.min.js) - 0.5 KB
Or use [npm] (https://npmjs.org/package/micromustache):
npm install micromustache
#Features
#Limitations
MicroMustache achieves its fast speed and light weight by dropping the following features from Mustache:
#How to use
MicroMustache is pretty similar to Mustache so if you know how to use Mustache, you can easily replace it with MicroMustache without any code change.
var person = {
first: "Alex",
last: "Ewerlöf"
};
//The render function accepts two parameters: the template and the object that contains a list of key-values to be replaced in template.
var output = MicroMustache.render("MicroMustache is created by {{first}} {{ last }}", person);
//output = "MicroMustache is created by Alex Ewerlöf"
Alternatively you can compile the template and get a function that can be used multiple times:
var templateEngine = MicroMustache.compile("MicroMustache is created by {{first}} {{ last }}");
output = templateEngine(person);
//output = "MicroMustache is created by Alex Ewerlöf"
output = templateEngine({first:'Albert',last:'Einstein'});
//output = "MicroMustache is created by Albert Einstein"
It is easy to access array elements:
var fruits = [ "orange", "apple", "lemon" ];
var output = MicroMustache.render("I like {{0}}, {{1}} and {{2}}", fruits);
//output = "I like orange, apple and lemon"
##Functions
The following functions from Mustache.js are supported:
MicroMustache.render(template,view);//renders a template based on the data in the view object and returns the rendered string
MicroMustache.to_html(template,view);//same as render
MicroMustache.compile(template);//returns a function that accepts the view object and spits out the rendered string
MicroMustache doesn't have any assumption about how it is used. If you want to use it as an AMD module, just copy/paste the contents of [micromustache.js] (https://raw.github.com/hanifbbz/micromustache/master/dist/micromustache.js) and replace the first line of code from:
var MicroMustache = (function () {
to define(function () {
and the last line of code from })();
to });
.
Then you can easily use it like:
require( 'MicroMustache', function ( MicroMustache ) {
alert(MicroMustache.render('{{var}}',{var:'val'}));
});
For more examples see the micromustache-test.js inside the test directory above.
#Algorithm
The algorithm behind MicroMustache is rather simple.
But for the sake of clarity, here is what the render()
function does:
{{...}}
view
object that matches what is between {{
and }}
{{...}}
with the value{{
and }}
and replace the {{...}}
with the return of the function{{...}}
part#Build
If you want to modify the code, here is how to do it.
npm
)npm install
gulp
and if there's no issue, it should make two new javascript files in the dist
directory.2.1.20 - 2014-07-11
FAQs
A fast, minimal and secure template engine for JavaScript
The npm package micromustache receives a total of 45,113 weekly downloads. As such, micromustache popularity was classified as popular.
We found that micromustache demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.