Simple and powerful swig. js, simple and powerful swig. js
Swig. js
A simple, powerful, and extendable JavaScript Template Engine.
Summary: JS template engine.
Why to use
- Rendering pages based on lujin
- Object-oriented template inheritance makes pages more reusable
- Dynamic page
- Quick Start
- Powerful functions
- Others
How to use
See swig. js Official Website
Reuse Project Instance pages
Most pages have headers and footer areas, which can be reused by inheriting pages, as follows:
Layout.html
Welcome.html
// Enter {% extends 'according to the actual directory '.. /layout.html '% }{% block content %} <div class = "content-container">
Configuration of information and functions Enterprise information, ownership, business registration number and other information, the information can be stored in the system-params.json, you can also add real-time computing functions, and then through swig. js display and call:
System-params.json:
{"IsDevMode": "true", "corporation": "CCCCCCCC", "ICPNumber": "Shanghai ICP filing xxxxxxxx ",...}
App. js
var systemParams = require('./config/system-params.json'), swig = require('swig');swig.setDefaults({ cache: !systemParams.isDevMode, locals: { now: function () { return new Date(); }, systemParams: systemParams }});
Layout.html
Integration of Express. js and Require. js In Express. js, a page is rendered through swig. js. The required js is loaded in the form of require. js require (deps, callback) in the page:
App. js
app.engine('html', swig.renderFile);app.set('view engine', 'html');app.set('views', __dirname + '/views');
Routers. js
module.exports = [{ path: '/', view: 'default/welcome', data: { title: 'welcome', requireScripts: [ 'controllers/default/welcome-controller' ], styles: [ 'default/welcome.css' ] }}];
Config-routers.js
var routers = require('./routers');...for ... { ... router.get(routers[i].path, function(req, res) { res.render(routers[i].view, routers[i].data, routers[i].callback); });}
Layout.html
<Html>
Bootstrap. js
// add others jsGlobalConfig.requireScripts.push('...');requirejs(GlobalConfig.requireScripts, function () { // todo}
Others Super Quick Start:
Swig. js docs