bob_cobb
bob_cobb

Reputation: 2269

Using jade.compile to compile a jade template that is a mixin file or has partials included

I'm trying to figure out how to do this.

Problem: I'm trying to compile a jade template for my email campaign but the file I'm trying to compile is a mixin which includes some partial.

e.g.:

controllers/user.js:

var emailTemplate = jade.compile(fs.readFileSync('./views/emails/new_user.jade', 'utf8'), { filename: './views/emails/new_user.jade'});

  var template = emailTemplate({
      baseUrl: res.locals.baseUrl,
      confirmCode: user.confirmCode,
      siteLogo: config.siteLogo,
      name: user.username,
      email: user.email
    }); 

./views/emails/new_user.jade:

include ../mixins/emails
div(style='margin-bottom: 20px; border: 1px solid #ddd; padding: 20px; width: 50%; margin: 0 auto 20px;')
  div(style='text-align: center; border-bottom: 1px solid #EEE; padding-bottom: 10px;')
    img(src='#{siteLogo}', style='text-align: center;')
  p
    | Hi #{name},
  p
    | Please confirm your account
  div(style='background-color: #179159; border-bottom: 1px solid #16814F; display: block; float: left; margin-bottom: 20px; text-align: center;       width: 100%;') 
  mixin button('test') 

./views/emails/mixins/emails.jade:

mixin button(text)
button
  =text

It looks like renderFile in jade.compile only opens one at a time, unfortunately.

https://github.com/visionmedia/jade/blob/master/jade.js#L950

Is there a way that I can do what I want (which is open new_user.jade which contains mixins) or do I have to do something like:

or something crazy?

Upvotes: 0

Views: 1309

Answers (1)

damphat
damphat

Reputation: 18956

Look at the source

exports.renderFile = function(path, options, fn){
  ...
  var str = options.cache
    ? exports.cache[key] || (exports.cache[key] = fs.readFileSync(path, 'utf8'))
    : fs.readFileSync(path, 'utf8');
  return exports.render(str, options);
};

cache is disable by default, so you should not modify renderFile

to enable cache:

renderFile('path/to/file.jade', {cache: true}) 

I do not know exactly your problem, but you need to separate compile-time and render-time. If you mean your jade files are changed during process, so you need to recompile them.

define mixin

mixin button(text)
  button= text

To call a mixin

+button('test') 

Upvotes: 2

Related Questions