npm install fastify-juicer --save
const fastify = require('fastify')()
fastify.register(require('fastify-juicer'))
fastify.get('/', (req, reply) => {
reply.view('/templates/index.html', { text: 'text' })
})
fastify.listen(3000, err => {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
})
If you want to set a fixed templates folder, or pass some options to the template engines:
fastify.register(require('point-of-view'), {
templates: 'templates',
includeViewExtension:"html"
})
Compile the template and render the result immediately based on the given data.
fastify.get('/', (req, reply) => {
reply.view('/templates/index.html', { text: 'text' })
})
Use ${} to output variable values, where _ is a reference to the data source (such as ${_}, often used for data source arrays). Supports custom functions (you can implement many interesting functions with custom functions, like ${data|links} You can assemble the data directly with the <a Href= by defining a predefined custom function links "..." alt= "..."/>).
${name}
${name|function}
${name|function, arg1, arg2}
Let's illustrate the amazing use of custom functions with an example.
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 + '" />';
};
juicer.register('links_build', links); //Registering a custom function juicer(tpl, json);
After the above code executes we will find that the result is this:
<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 />
As you can see, the result is escaped, and if we use $${item|links} we will get the result we expected, which is the avoidance of escaping that is going to be mentioned below.
Escape/Avoid escaping
For security reasons, the
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>juicer</strong>'
If you need to iterate over the array, you can use each one like this.
{@each list as item}
${item.prop}
{@/each}
It is also handy if you want to get the current index value during traversal.
{@each list as item, index}
${item.prop}
${index} //now index
{@/each}
We also often encounter the logical judgment of the data.
{@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}
For the maintainability and readability of subsequent code, we can add annotations to the template.
{# Here is the comment content}
The secondary loop is a juicer for you, and maybe you'll need it in some kind of situation.
{@each i in range(5, 10)}
${i}; //输出 5;6;7;8;9;
{@/each}
Child template nesting allows you to organize your template code in a more flexible manner, except that you can introduce templates that are specified in the data, but you can also use the template code written in the script tag by specifying the string #id.
html code
<script type="text/juicer" id="subTpl">
I'm sub content, ${name}
</script>
js code
var tpl = 'Hi, {@include "#subTpl", subData}, End.';
juicer(tpl, {
subData: {
name: 'juicer'
}
});
//output Hi, I'm sub content, juicer, End.
//or by introducing a child template with data, the following code will have the same render result:
var tpl = 'Hi, {@include subTpl, subData}, End.';
juicer(tpl, {
subTpl: "I'm sub content, ${name}",
subData: {
name: 'juicer'
}
});
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}
]
};
fastify.get('/', (req, reply) => {
reply.view('/templates/index.html', data)
})