Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Monkberry compile template to JavaScript code for creating nodes with DOM API and helper methods for updating content of these nodes.
npm install monkberry --save
Monkberry will compile this template:
<div>
<h1>{{ title }}</h1>
<p>
{{ text }}
</p>
</div>
To JavaScript code like this:
var div = document.createElement('div');
var h1 = document.createElement('h1');
var p = document.createElement('p');
div.appendChild(h1);
div.appendChild(p);
...
view.update = function (data) {
h1.textContent = data.title;
p.textContent = data.text;
};
Which can be used like that:
var view = monkberry.render('template');
document.body.appendChild(view.dom());
view.update({
title: 'Monkberry',
text: 'JavaScript DOM template engine'
});
Monkberry has support for both browserify via monkberrify and for webpack via monkberry-loader.
Monkberry can be used like CLI tool. Install Monkberry globally:
npm install monkberry -g
To compile all templates into single JavaScript file with source map run next command:
monkberry --source-map --output template.js templates/*.html
Require generated template.js
and monkberry.js
files and mount template:
var monkberry = require('monkberry');
var template = require('./template.js');
monkberry.mount(template);
Render that view.
var view = monkberry.render('template');
// or
var view = monkberry.render('template', {...});
Attach generated DOM nodes to the page.
document.getElementById('root').appendChild(view.dom());
Now, to update data of view on page:
view.update({...});
// or update only what's needed
view.update({key: value});
Monkberry perceives everything inside {{
and }}
mustache as JavaScript expression.
<div class="greetings {{ visible ? '' : 'hidden' }}">
Hello, {{ user.name + "!" }}
</div>
Can be any valid JavaScrpt expressions.
{% if count < 0 || count > 10 %}
...
{% else %}
...
{% endif %}
Any number on variables in if
:
{% if array.indexOf(search) != -1 %}
...
{% endif %}
Note what Monkberry update only one of
if
/else
block.{% if check %} Then {{ value }}! {% else %} Else {{ value }}! {% endif %}
Render that template:
var view = monkberry.render('example', { check: true, value: 'one' });
View will be
Then one!
. When if update view:
view.update({ check: false, value: 'two' });
View will be
Else two!
. But if update onlycheck
, variable of then part will be same as before.
view.update({check: true});
View will be
Then one!
.This is happens becouse Monkberry does not stores variables passed to
update
function, it stores only DOM nodes. Monkberry will update only one part ofif
/else
.
Monkberry can loop other arrays and objects as well.
{% for array %}
{{ name }}
{% endfor %}
In this form, body of for
has access only for variables iterating on.
view.update({
array: [
{name: 'Anton'},
...
]
});
To access outer scope specify iterator name.
{% for user of array %}
{{ user.name }}
{% endfor %}
Also key can be specified.
{% for key, user of array %}
{{ key }}: {{ user.name }}
{% endfor %}
Any expression support filter statement.
Hello, {{ user.name | upper }}
To define that filter:
monkberry.filters.upper = function (text) {
return text.toUpperCase();
};
Also Monkberry understand parameters for filters:
monkberry.filters.replace = function (text, from, to) {
return text.replace(from, to);
};
{{ text | replace(/.../, '$1') }}
And allow to combine filters:
{{ text | lower | replace(/.../, '$1') | upper }}
That expression will be compiled to next JavaScript:
upper(replace(lower(text), /.../, '$1'));
Filters can be used in expressions, if
and for
statements.
Any template mounted to Monkberry can be called as custom tag.
monkberry.mount(require('./views/block.html'));
Inside another template possible to insert that block
templace as custom tag:
<div>
<block/>
</div>
One file can contains several definitions of custom tags:
<my-tag>
...
</my-tag>
<my-second-tag>
...
</my-second-tag>
Custom tags may contains variables:
<greet>
{{ value }}, {{ name }}!
</greet>
To render that custom tag, specify variables as attributes:
<greet value="Hello" name="world">
<greet value="Hello" name="{{ user.name }}">
To speedup render Monkberry can prerender DOM nodes to use them in future.
monkberry.prerender('template', 10); // Preprender template 10 times.
Then next render
call will use one of these prerendered views:
monkberry.render('template', {...}); // Will use already created DOM nodes.
This is very usefull to do then browser waiting some xhr request.
To get info about prerendered template in runtime, use monkberry.pool.store
.
Every template in Monkbeery when rendered can be "wrapped" by function.
For example we have a template logo.html
:
<div>
<i class="svg-icon"></i>
</div>
And we want to insert SVG nodes inside i
tag on render. This is can be done via wrappers:
monkberry.wrappers.logo = function (view) {
view.dom().querySelector('.svg-icon').appendChild(svgIconNodes);
return view;
};
Wrappers usefull to manipulate view's nodes, adding event listeners and a lot of other staff.
Transformers allow to modify AST before compilation of templates. List of AST nodes can be founded here: ast.js Example of transform which trim whitespaces: whitespace.js
Add transforms to Monkbeery before compilation:
import { Compiler } from 'monkberry';
import { myTransform } from './myTransform';
var compiler = new Compiler();
compiler.transforms.custom = myTransform;
Now Monkberry support only one type of parser, mustage like (monk
named). But it can be extender with custom parsers. Every parser must return valid AST tree.
import { Compiler } from 'monkberry';
import { myParser } from './parser';
var compiler = new Compiler();
compiler.parsers.myParser = myTransform;
compiler.addSource('template', code, 'myParser');
compiler.addSource('another', code, 'monk');
var output = compiler.compile();
Monkberry uses Jasmine and testem. To run test locally run:
testem ci
Benchmarks covers a few use cases, and compare Monkberry with React and temple-wat. Also it's contains real site code for soft/hard update tests.
3.1.0
FAQs
Monkberry is a JavaScript library for building web user interfaces
The npm package monkberry receives a total of 445 weekly downloads. As such, monkberry popularity was classified as not popular.
We found that monkberry 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
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.