2016-05-01 154 views
0

有沒有辦法讓你的HTML輸出來自一個Jade模板被美化?類似於Express的app.locals.pretty = true;?如果相關,我正在使用koa-routerkoa-views與Koa漂亮的打印HTML輸出?

server.js

const koa = require('koa'); 
const views = require('koa-views'); 
const serve = require('koa-static'); 
const router = require('./routes'); 

const app = koa(); 

app.use(serve(`${__dirname}/public`)); 
app.use(views(`${__dirname}/views`, { extension: 'jade' })); 
app.use(router.routes()); 

app.listen(3000,() => { 
    console.log('Server listening at http://localhost:3000'); 
}); 

路線/ index.js

const router = require('koa-router')(); 

router.get('/', function *() { 
    yield this.render('index'); 
}); 

module.exports = router; 

的意見/ index.jade

html 
    head 
    title Hello 
    meta(charset='utf-8') 
    meta(name='viewport', content='width=device-width, initial-scale=1.0') 
    link(type='text/css', rel='stylesheet', href='css/style.css') 

    body 
    h1 Hi 

回答

0

你Ç烏爾德嘗試:

app.use(views(`${__dirname}/views`, { extension: 'jade', pretty:true})); 

應該做的伎倆......,另一方面 ,我使用KOA玉代替KOA-意見。我當前的代碼如下所示:

var Jade = require('koa-jade'); 

var jade = new Jade({ 
    viewPath: path.resolve(__dirname,"jade"), 
    debug: true, 
    pretty: true, 
    compileDebug: true, 
    basedir: path.resolve(__dirname,"jade"), 
    app:app 
    }) 

在生產模式下,我只是很設置爲false ...

HTH

+0

不幸的是'KOA-views'沒有漂亮的選項:( 我想我必須嘗試koa玉,謝謝。 – saadq