Brief introduction of Nodejs's highly scalable template engine functmpl
- 2021-07-21 05:42:10
- OfStack
About functmpl
functmpl is a highly scalable template engine based on JavaScript/Nodejs
Template format
<#包含/>
If you want to insert another template in one template, you can use the < # Includes/ >
For example, insert the template a. ftl:
<#"a.ftl"/>
<@功能/>
Besides output variables and including templates, you can also add more custom functions, such as enumeration and assignment
However, additional functions need to be customized.
The specific format of the custom function is
<
@ Function name parameter name = parameter value/
>
<
@ function name parameter name = parameter value
>
Child node
<
/
>
Where the parameter name/parameter value may have no or multiple pairs.
The parameter value can be a variable name or JSON
Child node content can be normal content, < = Output/ > , < # Includes/ > Or < @ Functions/ >
<`表达式`>
If you are only executing expressions, you can use the < ` Expression ' >
<`=输出`>
If you want to output the value of 1 variable, you can use the < = Output/ >
For example, the value of the output variable val
<\`=val\`>
How to use
Obtain functmpl
Use npm or git
npm install functmpl
git clone git@github.com:wangchenxunum/functmpl.git
git clone git@git.oschina.net:wangchenxunum/functmpl.git
Introduced to Nodejs
let functmpl = require('functmpl');
Introduced to the browser
<script src="functmpl.js"></script>
Template parser instance
// Create 1 Parser
let ftl = functmpl();
// Add function processor
ftl.use(functmpl.func);
// Template parsing
ftl.template = '<!DOCTYPE html>\n\
<html>\n\
<head>\n\
<title><`=title`></title>\n\
</head>\n\
<body>\n\
<@enum key="key" value="value" var=list>\n\
<@scope>\n\
<@set key="key" value="1" type="create"/>\n\
<`=key`>:<`=value`><br>\n\
</>\n\
<`=key`>:<`=value`><br>\n\
</>\n\
</body>\n\
</html>'
// Set the template position if you use <# Include /> , there must be 1 Basic relative position
ftl.filename = 'template.ftl';
// You can also read the template file directly
ftl.loadFile('template.ftl',function(status){
if (status) {
// The file was read successfully and parsed
// Call template to generate data
ftl.parse(function(text){
// Call the callback function when the generation is finished
console.log(" Build complete :\n" + text);
},JSON.parse(data.value));
} else {
// Failed to read file
}
});