Add inheritance to lodash/underscore template engine
npm install lodash-template-extrasIn browser:
``html`
In Node.js:
`js``
var _ = require('lodash');
require('lodash-remplate-extras')(_);
`js
//add a template
_.templateEx.add('layout', ''
+ '
+ 'Foo
'
+ '
+ '
//add some templates Hello world!
_.templateEx.add({
layout: '
+ 'Foo
'
+ '
+ '
page: '<% @extends("layout")%>'
+ '
+ '
+ '
});
//template can have alias
_.templateEx.add({
'long/long/name': '<% @alias("short")%>'
+ '
Use template
`js
_.templateEx.add({
hello: 'Hello <%=name%>!
'
});_.templateEx('hello',{
name: 'world'
});
// ➜
Hello world!
``Inheritance
`html
Foo
``html
<% @extends("layout")%>
:)
Hello world!
bar
``_.templateEx('page'); compiles to:`html
:)
Foo
Hello world!
bar
`Include
`html
<% @include("footer")%>
`
`html
`_.templateEx('page'); compiles to:`html
`Helper
`js
_.templateEx.addHelper('upper', function(str){
str.toUpperCase();
})
``
`html
Hello <% @upper(name)%>!
`_.templateEx('page', {name: 'world' }); compiles to:`html
Hello WORLD!
``