最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Templating language for both client-side and server-side rendering - Stack Overflow

programmeradmin4浏览0评论

I'm investigating JavaScript templates as a way to render our views. The goal is to be able to render client-side for users that have JavaScript enabled to to render the same templates/data server side for those users (and crawlers) that don't.

Requirements

  1. Client side rendering.
  2. Server side rendering (JVM support a nice-to-have).
  3. Reasonable support for loops, conditionals, text manipulation, partials, macros and extensions/plugins.
  4. Reasonably big/active munity of users.

Unsatisfactory solutions found so far

  1. Mustache: supports server and client side rendering, big/active munity, but the "logic-less" templates seem very restrictive and painful.
  2. Node.js plus underscore: JS templates like underscore provide all the template flexibility I need, but rendering them server-side using Node.js is VERY fragile. node.js is new and fairly unstable and its package management (exports...) requires lots of hacks for things to work properly.
  3. Google Closure Templates: these pile down to JS and Java and have a lot of the features I want. Unfortunately, browsing the Google groups, it looks like lots of basic features are missing (such as ability to loop over associative arrays) and the documentation for making your own plugins is very sparse and painful looking. Moreover, I can't tell who in the online munity is using it, Google isn't accepting open source contributions for it, and they don't have the bandwidth to release new features.
  4. isotope: write templates in JS and use Johnson, a Ruby wrapper for the Mozilla SpiderMonkey JS runtime, to render those templates server side. Seems promising, but the project hasn't had mits in over 6 months.
  5. Liquid with Liquid.js: the Liquid templating language looks superb, but the liquid.js project seems to be untested (no testing in IE!), has no documentation, it's not clear if anyone uses it, and it hasn't been updated in a while.

I'm investigating JavaScript templates as a way to render our views. The goal is to be able to render client-side for users that have JavaScript enabled to to render the same templates/data server side for those users (and crawlers) that don't.

Requirements

  1. Client side rendering.
  2. Server side rendering (JVM support a nice-to-have).
  3. Reasonable support for loops, conditionals, text manipulation, partials, macros and extensions/plugins.
  4. Reasonably big/active munity of users.

Unsatisfactory solutions found so far

  1. Mustache: supports server and client side rendering, big/active munity, but the "logic-less" templates seem very restrictive and painful.
  2. Node.js plus underscore: JS templates like underscore provide all the template flexibility I need, but rendering them server-side using Node.js is VERY fragile. node.js is new and fairly unstable and its package management (exports...) requires lots of hacks for things to work properly.
  3. Google Closure Templates: these pile down to JS and Java and have a lot of the features I want. Unfortunately, browsing the Google groups, it looks like lots of basic features are missing (such as ability to loop over associative arrays) and the documentation for making your own plugins is very sparse and painful looking. Moreover, I can't tell who in the online munity is using it, Google isn't accepting open source contributions for it, and they don't have the bandwidth to release new features.
  4. isotope: write templates in JS and use Johnson, a Ruby wrapper for the Mozilla SpiderMonkey JS runtime, to render those templates server side. Seems promising, but the project hasn't had mits in over 6 months.
  5. Liquid with Liquid.js: the Liquid templating language looks superb, but the liquid.js project seems to be untested (no testing in IE!), has no documentation, it's not clear if anyone uses it, and it hasn't been updated in a while.
Share Improve this question edited Mar 31, 2011 at 20:11 Yevgeniy Brikman asked Mar 31, 2011 at 2:52 Yevgeniy BrikmanYevgeniy Brikman 9,3816 gold badges49 silver badges62 bronze badges 1
  • 1 This is an interesting topic. Since you require the template language support both server and client side, I am a bit curious how a server side template engine correctly process the template on server side dynamic content and leave the client side content (including template instructions) untouched. – Gelin Luo Commented Sep 2, 2012 at 23:38
Add a ment  | 

3 Answers 3

Reset to default 3

Use EJS. It's pretty much got all the features you ask.

It can be used with express.js out of the box and it works nicely on the client.

As a bonus I would use backbone.js to do MVC on the client & server.

Of course this does rely on node.js. I feel that using express, now & backbone allows you a lot of stable control with node.js. Not to mention that SSJS is simply awesome and that code re-use is fantastic.

We ended up picking dust.js. Check out Leaving JSPs in the dust and The client-side templating throwdown for more info.

I would opt for XSLT (http://www.w3schools./xsl/xsl_client.asp)

发布评论

评论列表(0)

  1. 暂无评论