AGamePlayer
AGamePlayer

Reputation: 7732

How to correctly use Jade.render?

Recently I found that Jade is a very cool template.

But the API seems a bit confusing to me.

On its official page (http://jade-lang.com/api/) it defines the options object as follows:

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

But when I searched some other docs, I found that I can pass in a JSON object as injecting variables to my template, which would replace things like #{something_in_json.value}

And the JSON is like:

"something_in_json":"this is a value"}

My question is, what should I do if I want to use both features? I want to pass in JSON variables and also use the parameters (for example, I want to use pretty:true).

Upvotes: 0

Views: 101

Answers (1)

Rezigned
Rezigned

Reputation: 4942

There're 2 ways to do this

  1. Merge your JSON data with Jade's options

    // assuming jQuery is available
    $.extend(data, jade_options, json_data);
    jade.render(jade_source, data);
    
  2. Use Jade's compile function instead, a bit uglier

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

Upvotes: 1

Related Questions