我正在尝试使用 Node.js 和 Dust 作为模板引擎创建一个带有小部件的页面。我希望小部件尽可能独立,有自己的 css、js 和 html。
以下三种方法哪个最好?哪个最快?
选项 a:使用 res.render 并且只渲染一次
pseudo code:
1) call page route (app.get('/', routes.somePage);
2) load config file (say /views/somePage.cfg) listing all widgets on somePage.
3) get widget data if needed (say access another server for the data).
4) build 1 context object and pass to render
5) call res.render with context containing page data + all widget's data
5) somePage template will include partials to build page, including widget partials.
选项 b:使用 res.write
pseudo code:
1) call page route (app.get('/', routes.somePage);
2) load config file (say /views/somePage.cfg) listing all widgets on somePage.
3) use app.render() to render widgets + page (running js to get each widget data)
4) use res.write() to build a complete page.
选项 c:使用助手
pseudo code:
1) call page route (app.get('/', routes.somePage);
2) use app.render()
3) use custom helper to render different widgets.
custom helper will actually grab the data and might use app.render inside to build
the widget during render time..