te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Node says Jade has no method "renderFile", why? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node says Jade has no method "renderFile", why? - Stack Overflow

programmeradmin3浏览0评论

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: '[email protected]', isA: 'human' },
            tobi: { age: 1, email: '[email protected]', 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._pile (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)

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: '[email protected]', isA: 'human' },
            tobi: { age: 1, email: '[email protected]', 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._pile (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)
Share Improve this question asked Sep 3, 2011 at 10:08 corazzacorazza 32.4k39 gold badges120 silver badges191 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 14

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./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.pile(fs.readFileSync('code.jade', 'utf8'));

var html = jadetemplate({
  users: {
    tj: { age: 23, email: '[email protected]', isA: 'human' },
    tobi: { age: 1, email: '[email protected]', 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.

I recently came across the same issue. In the Alex Young tutorial I was following he was using jade.renderFile(). Similar to your case I was getting the same method not found message. After searching around I found that he had updated the function in a later mit. In this case he had created a custom function (renderJadeFile) as a "drop in" replacement for the jade.renderFile() function (See following code snippet).

Hope this helps others looking for a solution to this problem.

// Replacement function for jade.renderFile.
function renderJadeFile(template, options) {
  var fn = jade.pile(template, options);
  return fn(options.locals);
}

emails = {
  send: function(template, mailOptions, templateOptions) {
    mailOptions.to = mailOptions.to;
    // jade.renderFile(path.join(__dirname, 'views', 'mailer', template), templateOptions, function(err, text) {
    renderJadeFile(path.join(__dirname, 'views', 'mailer', template), templateOptions, function(err, text) {
      // Add the rendered Jade template to the mailOptions
      mailOptions.body = text;

      // CODE SHORTENED FOR BREVETIY
  },

  sendWele: function(user) {
    this.send('wele.jade', {
        to: user.email,
        subject: 'Wele to Nodepad'
      },
      { locals: {
        user: user
      }
    });
  }
};

The real solution, you need to pass an absolute path as the 1st parameter in the renderFile function. It has to be relative to the root. "code.jade" won't work. Here is a working example:

  jade.renderFile('/Users/tom/documents/xueqiu/pp-fe/views/a-share.jade', {name:'Shenxin Xu'}, function(err, html){
        console.log(1111 + ' ' + html);
      });

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论