# js-nano-template

> The *nano()* method replaces text like {user.lastname} with data from an JSON object like {user: {lastname: 'demo'}}.

Latest version **0.0.5** (published 2017-08-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install js-nano-template
pnpm add js-nano-template
yarn add js-nano-template
bun add js-nano-template
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.5 |
| Published | 2017-08-03 |
| First published | 2017-08-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Andreas Zettl |
| Maintainers | azettl |

## Links

- npm: https://www.npmjs.com/package/js-nano-template
- Repository: https://github.com/azettl/js-nano-template
- Homepage: https://github.com/azettl/js-nano-template#readme
- Issues: https://github.com/azettl/js-nano-template/issues
- npm.io page: https://npm.io/package/js-nano-template

## Recent versions

- 0.0.5 (latest) — 2017-08-03
- 0.0.4 — 2017-08-03
- 0.0.3 — 2017-08-03
- 0.0.1 — 2017-08-02

## README

NANO - Template Engine
=============================

[![Codacy Badge](https://api.codacy.com/project/badge/Grade/b709fa6c872242faa6c84e12c9f8d298)](https://www.codacy.com/app/azettl/nano?utm_source=github.com&utm_medium=referral&utm_content=azettl/nano&utm_campaign=badger)

## nano()

The *nano()* method replaces text like {user.lastname} with data from an JSON object like {user: {lastname: 'demo'}}.

## Installation
```cmd
  bower install js-nano-template
```

or 

```cmd
  npm install js-nano-template
```

or 

```cmd
  yarn add js-nano-template
```

**Syntax**

```javascript
  nano(t, d[, u])
```


**Parameters**

  *t* (string)   
  The template string in which the replacement should happen.

  *d* (object)   
  The JSON object.

  *u* (boolean)  
  Optional. Whether strings which can't be replaced should be shown.



  **Syntax**

  ```javascript
    nanoExternal(t, d[, u, e])
  ```


  **Parameters**

  *t* (string)   
  The template url in which the replacement should happen.

  *d* (object)   
  The JSON object.

  *u* (boolean)  
  Optional. Whether strings which can't be replaced should be shown.

  *e* (object)  
  Optional. dom element, if a dom element is provided the replaced text will be loaded in that instead of returned.


## Examples

Assuming you have following JSON response:

```json
data = {
  user: {
    login: "demo",
    first_name: "Anon",
    "last name": "Ymous",
    account: {
      status: "active",
      expires_at: "2016-12-31"
    },
    greeting: function(){
      return 'Hello';
    }
  }
}
```

you can do:

```javascript
  nano("&lt;p&gt;{user.greeting()} {user.first_name} {user.last name}! Your account is &lt;strong&gt;{user.account.status}&lt;/strong&gt; {user.nonexistingnode}&lt;/p&gt;", data)
```

and you get the following string:

```html
  &lt;p&gt;Hello Anon Ymous! Your account is &lt;strong&gt;active&lt;/strong&gt; &lt;/p&gt;
```

or you can do:

```javascript
  nano("&lt;p&gt;{user.greeting()} {user.first_name} {user.last name}! Your account is &lt;strong&gt;{user.account.status}&lt;/strong&gt; {user.nonexistingnode}&lt;/p&gt;", data, true)
```

and you get the following string:

```html
  &lt;p&gt;Hello Anon Ymous! Your account is &lt;strong&gt;active&lt;/strong&gt; {user.nonexistingnode}&lt;/p&gt;
```

You can now also load an external template via nanoExternal('test.template.html', data, true);
The parameters are the same except that the first parameter requires an url.

```javascript
  nanoExternal('test.template.html', data, true)
```

or you pass the optional dom node and the replaced text will be directly loaded into that node.

```javascript
  nanoExternal('test.template.html', data, true, document.getElementById('myId'))
```

---
_Source: https://npm.io/package/js-nano-template · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
