Sorry for digging up this thread, but I wrote about a way to do this a little while ago: http://www.edave.net/2012/02/25/sharing-pre-compiled-templates-between-server-and-client-with-hogan-js/
This method I employed is similar in concept to a number of points above, however the actual un-compiled contents of the template is never exposed to the browser. Nor are there any ajax requests made to retrieve templates. Instead the templates are pre-compiled on the server (once in production mode) for use there and another pass is done to create serialised/stringified versions of them to be sent to the browser in a script response. This has the benefit of allowing you to make totally SEO friendly pages on the server and for quick initial responses and then to be able to update that page as all of your templates are available on the client side. Using pushState for subsequent requests (on decent browsers) will create a seamless experience and less load on your server, especially seeing as you only need to transport json over the wire from then on. I've done a lot of work optimising this process recently and have will write another blog post on these updates soon. In the mean-time you can see the rough, but working, code here: https://github.com/dave-elkan/hogan-template-renderer Cheers ~Dave On Fri, Jan 27, 2012 at 10:12 PM, Jonathan Buchanan < [email protected]> wrote: > On 16 January 2012 10:37, Richard Marr <[email protected]> wrote: > > > > Are any of you sharing templates between client & server? Would be > > interested to hear how you've approached it. > > > > > > -- > > Richard Marr > > My #lazydev solution is to define templates entirely in code, so > there's no in-between step involved in sharing them between client and > server - a single module can contain all the view functions, URL > mappings and templates it needs, e.g. this one [1] for an auto-admin > type app, which you can see an example of this being used client-side > here [2] (if I haven't busted the dependencies with recent changes, > you can also run it using Express and turn JavaScript off for a good > old forms 'n links experience). > > To serve a full page using the same template on the server-side, I can > just override one of the base templates to extend a full-page version > instead [3] - this solution should work for any template engine which > uses inheritance, compiles to JavaScript and allows you to tinker with > or replace any of the template objects afterwards. > > -- Jonny > > [1] https://github.com/insin/sacrum/blob/master/lib/sacrum/admin.js > [2] > http://jonathan.buchanan153.users.btopenworld.com/sacrum/fragile/fragile.html > [3] https://github.com/insin/sacrum/blob/master/fragile/index.js#L50-70 > > -- > Job Board: http://jobs.nodejs.org/ > Posting guidelines: > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > You received this message because you are subscribed to the Google > Groups "nodejs" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/nodejs?hl=en?hl=en > -- http://www.edave.net https://github.com/dave-elkan Twitter: @edave -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
