开发者

Recursive page assembling in Node.js with Express and Jade

I've been working on an API in Node.js for the first time, and of course I needed a test page so I decided to whip one up in Node as well for the hell of it.

After wracking my mind to come up with a good way to load the header, body and footer files (Jade syntax files) and have them be friends and render together, I came up with a recursive solution.

function assemblePage(name,markup)
{
    markup = markup || '';
    if (markup=='')
        fs.readFile('header.jade', function(err,data){assemblePage(name,markup+data)});
    else if (name != 'footer')
        fs.readFile(name+'.jade', function(err,data){assemblePage('footer',markup+data)});
    else
        fs.readFile开发者_JAVA技巧('footer.jade', function(err,data){console.log(markup+data);__res.send(jade.render(markup+data))});
}

So all I have to call is:

assemblePage('home');

Is this the best way to go about things?


I think you should be using expressjs(High performance, high class web development for Node.js) to render your templates. It has a very sophisticated View Rendering. I think what you need is called view partials. In the screencasts section you can watch a screencast about view partials

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜