<script type="text/javascript" src="juicer-min.js></script>
juicer(tpl, data);
var compiled_tpl = juicer(tpl);
var compiled_tpl = juicer(tpl); var html = compiled_tpl.render(data);
juicer.register('function_name', function); juicer.unregister('function_name');
juicer.set({ 'tag::operationOpen': '{@', 'tag::operationClose': '}', 'tag::interpolateOpen': '${', 'tag::interpolateClose': '}', 'tag::noneencodeOpen': '$${', 'tag::noneencodeClose': '}', 'tag::commentOpen': '{#', 'tag::commentClose': '}' });
{ cache: true [false], strip: true [false], errorhandling: true [false], detection: true [false] }
juicer.set('strip',false); juicer.set('cache',false);
juicer.set({ 'strip': false, 'cache': false };
${name} ${name|function} ${name|function, arg1, arg2}
var json = { links: [ {href: ' http://juicer.name ', alt: 'Juicer'}, {href: ' http://benben.cc ', alt: 'Benben'}, {href: ' http://ued.taobao.com ', alt: 'Taobao UED'} ] }; var tpl = [ '{@each links as item}', '${item|links_build} <br />', '{@/each}' ].join(''); var links = function(data) { return '<a href="' + data.href + '" alt="' + data.alt + '" />'; };
< a href=" http://juicer.name" ; alt=" Juicer" < br /> < a href=" http://benben.cc" ; alt=" Benben" < br /> < a href=" http://ued.taobao.com" ; alt=" Taobao UED" < br />
var json = { value: '< strong> juicer</ strong>' }; var escape_tpl='${value}'; var unescape_tpl='$${value}'; juicer(escape_tpl, json); // Output '< strong> juicer</ strong>' juicer(unescape_tpl, json); // Output '<strong>judge</strong>'
{@each list as item} ${item.prop} {@/each}
{@each list as item, index} ${item.prop} ${index}//Current index {@/each}
{@each list as item,index} {@if index===3} the index is 3, the value is ${item.prop} {@else if index === 4} the index is 4, the value is ${item.prop} {@else} the index is not 3, the value is ${item.prop} {@/if} {@/each}
{# Here is the comment content}
{@each i in range(5, 10)} ${i}; // Output 5; 6; 7; 8; 9; {@/each}
<script type="text/juicer" id="subTpl"> I'm sub content, ${name} </script>
var tpl = 'Hi, {@include "#subTpl", subData}, End.'; juicer(tpl, { subData: { name: 'juicer' } }); //Output Hi, I'm sub content, judge, End //Or, the following code will have the same rendering result by importing data into the sub template: var tpl = 'Hi, {@include subTpl, subData}, End.'; juicer(tpl, { subTpl: "I'm sub content, ${name}", subData: { name: 'juicer' } });
Execute on the command line: npm install juicer In the code, it is introduced as follows: var juicer = require('juicer'); var html = juicer(tpl, data);
npm install juicer var juicer = require('juicer'); app.set('view engine', 'html'); app.register('.html', { compile: function(str, options) { return juicer.compile(str, options).render; } });
npm install juicer var juicer = require('juicer'); var fs = require('fs'); app.set('view engine', 'html'); app.engine('html', function(path, options, fn){ fs.readFile(path, 'utf8', function(err, str){ if (err) return fn(err); str = juicer(str, options); fn(null, str); }); });
var juicerExpressAdapter = require('juicer-express-adapter'); app.set('view engine', 'html'); app.engine('html', juicerExpressAdapter);
npm install -g juicer juicer example.juicer.tmpl -f example.js // type `juicer` after install for more help. //After installing 'juicer' in global mode, enter 'juicer' at the command line to get more help information.
var juicer = require('juicer'); var LRUCache = require('lru-native'); var cache = new LRUCache({ maxElements: 1000 }); juicer.set('cachestore', cache);
HTML code: <script id="tpl" type="text/template"> <ul> {@each list as it,index} <li>${it.name} (index: ${index})</li> {@/each} {@each blah as it} <li> num: ${it.num} <br /> {@if it.num==3} {@each it.inner as it2} ${it2.time} <br /> {@/each} {@/if} </li> {@/each} </ul> </script> Javascript code: var data = { list: [ {name:' guokai', show: true}, {name:' benben', show: false}, {name:' dierbaby', show: true} ], blah: [ {num: 1}, {num: 2}, {num: 3, inner:[ {'time': '15:00'}, {'time': '16:00'}, {'time': '17:00'}, {'time': '18:00'} ]}, {num: 4} ] }; var tpl = document.getElementById('tpl').innerHTML; var html = juicer(tpl, data);