Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/mrrodrigo/html-builder

Very simple html builder
https://github.com/mrrodrigo/html-builder

builder html javascript npm

Last synced: about 1 month ago
JSON representation

Very simple html builder

Awesome Lists containing this project

README

        

# Simple html builder for js

![](https://img.shields.io/bundlephobia/min/simple-html-builder)
![](https://img.shields.io/npm/dy/simple-html-builder)
![](https://img.shields.io/npm/v/simple-html-builder)
```
npm i simple-html-builder
```

Each html tag has two functions, eg: using p tag:

```javascript

const html = new HtmlBuilder();

html.p(); // this function stack p tag and resolve on the build function

html.innerp(); // this function auto build and close the p tag

```

For tag functions you can pass 3 args:

- `attributes` (Object) => here you can set all params to you tag like class, style and key

- `innerContent` (String) => here you can set the inner text for this tag

- `options` (Object) => Only option is a boolean for `close`. When set this to true this tag is closed immediately and not respect the stack of tags. (works only for non "inner" functions)

For build simple html string:

```javascript

const HtmlBuilder = require('../index.js');

const html = new HtmlBuilder()
.div({ class: 'test' })
.p({}, 'test')
.build();

console.log(html);

//output

//

test

```

For build html with inner tags with custom params like class and styles.

```javascript

const HtmlBuilder = require('../index.js');

const html = new HtmlBuilderhtmlBuilder()
.tr()
.td(
{ class: 'column' },
htmlBuilder.innerp({ class: 'larger' }, 'date'),
{ close: true }
)
.td(
{ class: 'column' },
htmlBuilder.innerdiv(
{ style: 'text-align: center;' },
htmlBuilder.innerimg({
width: '23',
src: 'src',
})
)
)
.p(
{
style: 'margin: 5px 0; text-align: center;',
},
htmlBuilder.innera(
{ class: 'link', href: 'test' },
'test'
),
{ close: true }
)
.build();

console.log(html);

//output (formatted)


date





test


```