Monkberry API strictly follows semantic versioning.
Returns a new Monkberry
template instance, a “view”.
template
— Monkberry
template object,node
— Element
DOM node where to attach the rendered template,options
— Object
optional options.const view = Monkberry.render(Template, document.body);
Options object:
noCache
— Boolean
to use the pool of prerendered templates or not,context
— Object
this will pass through every component hierarchy,filters
— Object
filters to template,directives
— Object
of directives.Prerender template for future usage.
template
— Monkberry
- template objecttimes
— Number
- how many timesMonkberry.prerender(Template, 10);
// Will return view with already created DOM nodes.
const view = Monkberry.render(Template, document.body);
This can be very useful. For example you can prerender templates while waiting for an XHR response.
Append rendered view to the specified node.
toNode
— Element
- DOM nodeInsert rendered view before the specified node.
toNode
— Element
- DOM node.Return view’s nodes. Note that if your template contains more than one root element, createDocument
will return DocumentFragment
that contains all of those nodes. If you have only one root node, it will be returned as is.
Update rendered template with a new state. You can specify only a part of state to update or you can update the entire state.
state
— Object
- values to update in templateExample:
const state = {
title: 'Title #1',
content: '...'
};
view.update(state);
// Update only title.
view.update({title: 'Title #2'});
Remove view’s nodes from document, and put it to pool for future reuse.
Select node by query.
query
— String
query to select nodeNote that this function uses Element.matches() for checking root nodes. Include polyfill for matches if you use it.
If your template contains more than one node on first level, querySelector
will search all subtrees. Array of all top level nodes can be accessed with view.nodes[]
array.
Note that querySelector cannot work with template which has if/for/custom node on first level.
{% if cond %} ... {% endif %}
You will get an exception like this:
Cannot use querySelector with non-element nodes on first level.
You can solve this by wrapping everything into another node:
<div> {% if cond %} ... {% endif %} </div>