节点说Jade没有方法“renderFile”,为什么? [英] Node says Jade has no method "renderFile", why?

查看:108
本文介绍了节点说Jade没有方法“renderFile”,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了jade(npm install jade),然后转到他们的github页面来获取一些示例。这就是我想要执行的内容:

code.jade:

   -  var title =Things
h1 = title
ul#users
- 每个用户,用户名称
- if(user.isA ==ferret)
li(class:'user-'+ name)#{name}只是一个雪貂
- else
li(class:'user-'+ name)#{name}#{user .email}

code.js:

  var jade = require('jade'); 

var options = {
locals:{
users:{
tj:{age:23,email:'tj@vision-media.ca',isA :'human'},
tobi:{age:1,email:'tobi@is-amazing.com',isA:'ferret'}
}
}
} ;

console.log(jade)

jade.renderFile('code.jade',options,function(err,html){
if(err)throw err;
console.log(html);
});

我将这些文件保存在自己的文件夹中,然后按照我的方式执行节点代码。 JS。然而,节点抛出一个错误,并说翡翠没有方法renderFile!你能告诉我我做错了什么,我应该怎么做才能解决它?



完整的错误信息:

  node.js:134 
throw e; // process.nextTick错误或第一次打勾时发生'错误'事件
^
TypeError:Object#< Object>在Object中没有方法'renderFile'
< anonymous> (/home/yann/javascript/jade/code.js:18:6)
在Module._compile(module.js:402:26)
在Object..js(module.js:408 :10)
at Module.load(module.js:334:31)
at Function._load(module.js:293:12)
at Array。< anonymous> (module.js:421:10)
在EventEmitter._tickCallback(node.js:126:26)


https://github.com/visionmedia/jade



类似这样的事情可能就是你想要的。只记得你只需要读一次文件。如果你是动态的,请不要同步阅读。

  var jade = require('jade'); 
var fs = require('fs');

var jadetemplate = jade.compile(fs.readFileSync('code.jade','utf8'));

var html = jadetemplate({
users:{
tj:{age:23,email:'tj@vision-media.ca',isA:'human'}} ,
tobi:{age:1,email:'tobi@is-amazing.com',isA:'ferret'}
}
});

console.log(html);



更新



这个答案在它写道,然而 renderFile 几个月后在 92c314 ,所以现在可以使用。


I installed jade (npm install jade) and went over to their github page to grab some examples. This is what I wanted to execute:

code.jade:

- var title = "Things"
h1= title
ul#users
  - each user, name in users
    - if (user.isA == "ferret")
      li(class: 'user-' + name) #{name} is just a ferret
    - else
      li(class: 'user-' + name) #{name} #{user.email}

code.js:

var jade = require('jade');

var options = {
    locals: {
        users: {
            tj: { age: 23, email: 'tj@vision-media.ca', isA: 'human' },
            tobi: { age: 1, email: 'tobi@is-amazing.com', isA: 'ferret' }
        }
    }
};

console.log(jade)

jade.renderFile('code.jade', options, function(err, html){
    if (err) throw err;
    console.log(html);
});

I saved those files in their own folder, cd'd my way there and executed "node code.js". However, node throws an error and says that Jade has no method "renderFile"! Can you tell me what am I doing wrong and what should I do to fix it?

full error message:

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
TypeError: Object #<Object> has no method 'renderFile'
    at Object.<anonymous> (/home/yann/javascript/jade/code.js:18:6)
    at Module._compile (module.js:402:26)
    at Object..js (module.js:408:10)
    at Module.load (module.js:334:31)
    at Function._load (module.js:293:12)
    at Array.<anonymous> (module.js:421:10)
    at EventEmitter._tickCallback (node.js:126:26)

解决方案

It looks like newer versions of Jade use a different API, there is no more 'renderFile' method. Take a look at the 'Public API' section on here: https://github.com/visionmedia/jade

Something like this is probably what you want. Just remember you only need to read the file once. If you are doing it dynamically, be sure not to read it synchronously.

var jade = require('jade');
var fs = require('fs');

var jadetemplate = jade.compile(fs.readFileSync('code.jade', 'utf8'));

var html = jadetemplate({
  users: {
    tj: { age: 23, email: 'tj@vision-media.ca', isA: 'human' },
    tobi: { age: 1, email: 'tobi@is-amazing.com', isA: 'ferret' }
  }
});

console.log(html);

Update

This answer was valid when it was written, however renderFile was added back in several months later in 92c314, so it may be used now.

这篇关于节点说Jade没有方法“renderFile”,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆