![PyPI Now Supports iOS and Android Wheels for Mobile Python Development](https://cdn.sanity.io/images/cgdhsj6q/production/96416c872705517a6a65ad9646ce3e7caef623a0-1024x1024.webp?w=400&fit=max&auto=format)
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Library to handle <template>
elements. Html templates is a method supported by all modern browsers that is faster than any other template system because it works with parsed html elements instead strings. Here's more info
Note: It's not supported by explorer
Download the package using npm, yarn or bower:
npm install domplates
yarn install domplates
bower install domplates
Include the library in the html:
<script type="text/javascript" src="domplate/src/index.js"></script>
Or import the module using AMD/CommonJS:
var Domplates = require('domplates');
var tmpl = new Domplates();
To render a template, you only need the id
of the template and an object with the data to use. The keys in the object are css selectors and the value is the data used. For example:
<template id="tmpl-welcome">
<p>Hello, <strong></strong>!</p>
</template>
const welcome = tmpl.render('tmpl-welcome', {
strong: 'World'
});
//Insert the result in the dom
document.body.appendChild(welcome);
<p>Hello, <strong>World</strong>!</p>
The third argument specify whether the result must be inserted in the dom or not. Set true
to insert the result just before the <template>
element:
tmpl.render('tmpl-welcome', {
strong: 'World'
}, true);
<p>Hello, <strong>World</strong>!</p>
<template id="tmpl-welcome">
<p>Hello, <strong></strong>!</p>
</template>
Or pass a Node
instance that will work as the container:
const container = document.getElementById('container');
tmpl.render('tmpl-welcome', {
strong: 'World'
}, container);
(*) The container will be emptied before insert the new content.
You can use an object instead a string to edit not only the content of the node but also its attributes. Example:
tmpl.render('tmpl-welcome', {
strong: {
html: 'World',
title: 'The title of the element'
}
}, true);
<p>Hello, <strong title="The title of the element">World</strong>!</p>
Use an array to create a new node for each value:
<template id="tmpl-list">
<ul>
<li></li>
</ul>
</template>
tmpl.render('tmpl-list', {
li: [
'Laura',
'Miguel',
'Guille'
]
}, true);
<ul>
<li>Laura</li>
<li>Miguel</li>
<li>Guille</li>
</ul>
And, of course, you can use an array of objects:
tmpl.render('tmpl-list', {
li: [
{
html: 'Laura',
class: 'is-girl'
},{
html: 'Miguel',
class: 'is-boy'
},{
html: 'Guille',
class: 'is-boy'
}
]
}, true);
<ul>
<li class="is-girl">Laura</li>
<li class="is-boy">Miguel</li>
<li class="is-boy">Guille</li>
</ul>
Nodes with the values null
, undefined
or false
will be removed:
<template id="tmpl-hello">
<p>Hello <strong></strong></p>
</template>
tmpl.render('tmpl-hello', {
strong: null
}, true);
<p>Hello</p>
(*) Use empty string if you want to keep empty nodes.
You can use also templates within templates:
<template id="tmpl-users">
<h1></h1>
<ul>
<template id="tmpl-user">
<li>
<a>
<strong></strong>
</a>
<p></p>
</li>
</template>
</ul>
</template>
tmpl.render('tmpl-users', {
h1: 'List of users',
'#tmpl-user': [
{
strong: 'Laura',
a: {href: 'http://laura.com'},
p: 'Web developer'
},{
strong: 'Miguel',
a: {href: 'http://miguel.com'},
p: 'UX designer'
},{
strong: 'Guille',
a: {href: 'http://guille.com'},
p: 'Dancing'
}
]
}, true);
<h1>List of users</h1>
<ul>
<li>
<a href="http://laura.com">
<strong>Laura</strong>
</a>
<p>Web developer</p>
</li>
<li>
<a href="http://miguel.com">
<strong>Miguel</strong>
</a>
<p>UX designer</p>
</li>
<li>
<a href="http://guille.com">
<strong>Guille</strong>
</a>
<p>Dancing</p>
</li>
</ul>
When the value of a node or attribute is a function, it will be evaluated. Note that the arguments passed to the function are the node element and the index.
<template id="tmpl-users">
<ul>
<li></li>
</ul>
<p>First paragraph</p>
<p>Second paragraph</p>
</template>
tmpl.render('tmpl-welcome', {
p: {
class: function (el, index) {
return 'position-' + index;
}
},
li: function () {
return [
'Laura',
'Miguel',
'Guille',
]
}
}, true);
<ul>
<li>Laura</li>
<li>Miguel</li>
<li>Guille</li>
</ul>
<p class="position-0">First paragraph</p>
<p class="position-1">Second paragraph</p>
Any attribute starting with on
and a function as value will be considered an event:
<template id="tmpl-actions">
<div class="buttons">
<button class="button"></button>
</div>
</template>
tmpl.render('tmpl-actions', {
button: [
{
html: 'Click me!'
onclick: function (event) {
event.preventDefault();
alert('Button clicked!')
}
},
{
html: 'Other button!'
onclick: function (event) {
event.preventDefault();
alert('Other button clicked!')
}
}
]
}, true);
<div class="buttons">
<button class="button">Click me!</button>
<button class="button">Other button!</button>
</div>
The attribute data
is used to save values in the dataset
property:
<template id="tmpl-actions">
<button class="button"></button>
</template>
tmpl.render('tmpl-actions', {
button: {
html: 'Click me!',
onclick: function (event) {
alert('Hello ' + this.dataset.name);
},
data: {
name: 'Miguel'
}
}
}, true);
<div class="buttons">
<button class="button">Click me!</button>
<button class="button">Other button!</button>
</div>
FAQs
HTML templates handled easily
We found that domplates 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
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.