如何正确使用Jade.render?

时间:2013-12-23 05:35:46

标签: pug

最近我发现Jade是一个非常酷的模板。

但API对我来说似乎有点混乱。

在其官方页面(http://jade-lang.com/api/)上,它定义了options对象,如下所示:

{
  filename:string
  pretty:boolean
  self:boolean
  debug:boolean
  compileDebug:boolean
  compiler:class
  globals:array
}

但是当我搜索其他一些文档时,我发现我可以传入一个JSON对象作为向我的模板注入变量,这将取代像#{something_in_json.value}

这样的东西

JSON就像:

"something_in_json":"this is a value"}

我的问题是,如果我想使用这两种功能,该怎么办?我想传入JSON变量并使用参数(例如,我想使用pretty:true)。

1 个答案:

答案 0 :(得分:1)

有两种方法可以做到这一点

  1. 将您的JSON数据与Jade的选项合并

    // assuming jQuery is available
    $.extend(data, jade_options, json_data);
    jade.render(jade_source, data);
    
  2. 使用Jade的编译功能,有点丑陋

    var fn = jade.compile(jade_source, jade_options);
    fn(data); // returns compiled jade template e.g. <html>...