繁体   English   中英

使用jade.compile编译一个作为mixin文件或包含部分的jade模板

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

我正在试图弄清楚如何做到这一点。

问题:我正在尝试为我的电子邮件广告系列编译玉模板,但我正在尝试编译的文件是mixin,其中包含一些部分内容。

例如:

控制器/ 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

看起来jade.compile中的renderFile只会一次打开一个,不幸的是。

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

有没有办法可以做我想做的事情(这是打开包含mixins的new_user.jade)或者我必须做以下事情:

  • 修改renderFile源并将其更改为接受数组
  • 在文件上执行fs.readFileSync()
  • 解析顶部的每一行检查include
  • 然后打开每个文件
  • 将它们连成一个.jade文件

还是疯狂的东西?

看看来源

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);
};

默认情况下,缓存处于禁用状态 ,因此您不应修改renderFile

启用缓存:

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

我不确切地知道你的问题,但你需要分开编译时渲染时间 如果您的意思是您的jade文件在处理过程中被更改,那么您需要重新编译它们。

定义mixin

mixin button(text)
  button= text

打电话给mixin

+button('test') 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM