Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile jade templates in to JavaScript functions to use them on client side?

Tags:

node.js

pug

I want to use compiled jade templates on client side. How should I compile them to get javascript files ? https://github.com/visionmedia/jade

like image 243
Oleg Dats Avatar asked Mar 01 '12 19:03

Oleg Dats


People also ask

Is Jade client side or server-side?

Jade is designed primarily for server-side templating in node.

Why use Jade template engine?

Jade is an elegant templating engine, primarily used for server-side templating in NodeJS. In plain words, Jade gives you a powerful new way to write markup, with a number of advantages over plain HTML.


2 Answers

Yes you can! https://github.com/techpines/asset-rack#jadeasset

I just open sourced "asset-rack", a nodejs project that can can precompile jade templates and serve them in the browser as javascript functions.

This means that rendering is blazingly fast, even faster then micro-templates because there is no compilation step in the browser.

First you set it up on the server as follows:

new JadeAsset({
    url: '/templates.js',
    dirname: __dirname + '/templates'
});

If you template directory looked like this:

templates/
  navbar.jade
  user.jade
  footer.jade

Then all your templates come into the browser under the variable "Templates":

$('body').append(Templates.navbar());
$('body').append(Templates.user({name: 'mike', occupation: 'sailor'});
$('body').append(Templates.footer());
like image 114
Brad C Avatar answered Oct 17 '22 03:10

Brad C


#coffeescript
jade = require 'jade'
data = '#menu'
options = 
  client: true
  compileDebug: false
fn = jade.compile data, options
console.log fn.toString()
like image 34
makoven Avatar answered Oct 17 '22 02:10

makoven