Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mateusmaso/handlebars.element

Handlebars plugin for declaring custom elements and attributes
https://github.com/mateusmaso/handlebars.element

custom-elements handlebars javascript

Last synced: 23 days ago
JSON representation

Handlebars plugin for declaring custom elements and attributes

Awesome Lists containing this project

README

        

handlebars.element [![Build Status](https://travis-ci.org/mateusmaso/handlebars.element.svg?branch=master)](https://travis-ci.org/mateusmaso/handlebars.element)
==================

This is a Handlebars plugin which allows using a similar W3C syntax for declaring custom elements and attributes inside templates.

## Install

```
$ npm install --save handlebars.element
```

## Usage

```javascript
var Handlebars = require("handlebars");
require("handlebars.element").default(Handlebars);

var main = document.querySelector("main");
var context = {};
var template = Handlebars.templates["path/to/template"];
var nodes = Handlebars.parseHTML(template(context));

nodes.forEach((node) => main.appendChild(node));
```

## Examples

### Declaring custom elements and attributes

```html


Now you can write custom elements and attributes with Handlebars!







```

### Registering a custom element

```javascript
Handlebars.registerElement("foo", function(attributes) {
var div = document.createElement("div");
div.innerText = "Hello World " + (attributes.title ? attributes.title : "guest");

if (attributes.red) {
div.className = "red";
} else if (attributes.green) {
div.className = "green";
} else if (attributes.blue) {
div.className = "blue";
}

return div;
}, {
booleans: ["red", "green", "blue"]
});
```

### Registering a custom attribute

```javascript
Handlebars.registerAttribute("bar", function(element) {
var style = document.createAttribute("style");
style.className = "purple";
return style;
}, {
ready: function(element) {
// Use this callback for listening to element events.
}
});
```

### After ```parseHTML```

```html


Now you can write custom elements and attributes with Handlebars!


Hello World Red

Hello World Green

Hello World Blue

Hello World Purple


```

## License

MIT © [Mateus Maso](http://www.mateusmaso.com)