我爲此made a dust helper。我把它叫做{@query}
這裏是它的簽名:
{@query string="que=ry&str=ing"/}
它融合que=ry&str=ing
實際req.query
參數,因此在前面的例子中,我們是在http://localhost:3000/search?q=foo&sort=asc
:
<a rel="next" href="?{@query string="page=2"/}">Next</a>
將輸出:
<a rel="next" href="?q=foo&sort=asc&page=2">Next</a>
-
具體的實現是遵循(中間件內能夠獲得req.query
):
var dust = require('dustjs-linkedin');
var _ = require('underscore');
var qs = require('querystring');
app.use(function(req, res, next) {
//
// Query helper for dust
//
// Merge querystring parameters to the current req.query
//
// Suppose we are on localhost:3000/search?q=foo :
// - {@query string=""/} will output q=foo
// - {@query string="bar=baz"/} will output q=foo&bar=baz
// - {@query string="q=fooo&bar=baz"/} will output q=fooo&bar=baz (notice fooo takes precedence)
//
dust.helpers.query = function (chunk, ctx, bodies, params) {
var str = dust.helpers.tap(params.string, chunk, ctx);
// Parse string="" parameter
var o = qs.parse(str);
// Merge with req.query
o = _.extend({}, req.query, o);
return chunk.write(qs.stringify(o));
}
next();
});
這應該在客戶端的工作:如何參數添加到已包含其他參數,也許一個錨URL]( http://stackoverflow.com/questions/6953944/how-to-add-parameters-to-a-url-that-already-contains-other-parameters-and-maybe)。也許你可以適應它在快速路線或模板中工作? –