Skip to content

Commit

Permalink
update readme and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanLiu0235 committed Feb 9, 2018
1 parent 47389fc commit df830ca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ A simple HTML template engine, currently a parser. Basically implements ejs synt
You can simply run the [examples here](https://github.com/stop2stare/leopard/tree/master/examples).

``` js
var leo = require('leopard-template')
var Leopard = require('leopard-template')
var leo = new Leopard()
var tpl = '<p>I am <%= name %>!</p>'
var data = {
name: 'Leopard'
}

var html = leo(tpl, data) // '<p>I am Leopard!</p>'
var html = leo.compile(tpl, data) // '<p>I am Leopard!</p>'
```

## Usage
Expand All @@ -27,12 +28,13 @@ $ npm install leopard-template
Of course you can import **leopard-template** in whatever way you want
``` js
// ES6 import
import leo from 'leopard-template'
import Leopard from 'leopard-template'

// CommonJS require
var leo = require('leopard-template')
var Leopard = require('leopard-template')

// and then you can start render your templates
// var leo = new Leopard()
```

Or you can also load with html `script` tag
Expand Down Expand Up @@ -92,6 +94,26 @@ var loops = 'Now I repeat: ' +
'</ul>'
```

## Filters

Filters are now supported in Leopard, you can custom a filter with `Leopard.filter`:

``` js
var Leopard = require('leopard-template')
Leopard.filter('toUpperCase', function(string) {
return string.toUpperCase()
})

var text = '<p><%= 'leopard' | toUpperCase %></p>' // <p>LEOPARD</p>
```

And also, filters can be chained:

``` js
// `reverse` is a preset filter
var text = '<p><%= 'leopard' | toUpperCase | reverse %></p>' // <p>DRAPOEL</p>
```

## Test

``` shell
Expand Down
4 changes: 2 additions & 2 deletions examples/bookDetail/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
{ "count": 6, "name": "外国文学" }
]
}

var html = Leopard(tpl, detail)
var leo = new Leopard()
var html = leo.compile(tpl, detail)
document.getElementById('app').innerHTML = html

</script>
Expand Down
8 changes: 6 additions & 2 deletions examples/listview/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
'</ul>' +
'<ul style=\"float:left;\">' +
'<% for (var i = 0; i < 20; i++) { %>' +
'<li><%= i %>: I am <%- name %>!</li>' +
'<li><%= i %>: I am <%- name | toUpperCase %>!</li>' +
'<% } %>' +
'</ul>'
var data = {
name: '<em>Leopard</em>'
}
var html = Leopard(tpl, data)
Leopard.filter('toUpperCase', function(string) {
return string.toUpperCase()
})
var leo = new Leopard()
var html = leo.compile(tpl, data)
document.getElementById('app').innerHTML = html

</script>
Expand Down

0 comments on commit df830ca

Please sign in to comment.