简体   繁体   中英

javascript in jade

Hello I'm trying to use some javascript built in functions inside jade but I get an error. Here is the code:

 - each post in posts
           li(class: 'user-') #{post.created} #{post.body} #{post.title} #{post.tags} #{post._id}
             - var tags = post.tags
             - tags.split(' ')
             - each tag in tags    
           li(class : 'tags') #{tags.tag}

I get the error : Object tag1,,,,,,,,tag2 has no method 'split'

It looks like you mixing tabs and spaces. Jade.js strongly follows the 2 space convention, and tabs ofetn mess up the parser. Removing the tabs everything works fine for me.

- each post in posts
  li(class: 'user-') #{post.created} #{post.body} #{post.title} #{post.tags} #{post._id}
   - var tags = post.tags.split(' ')
   - each tag in tags
     li(class : 'tags') #{tags.tag}

And the Javascript to render the .jade file.

var jade = require('jade');

var options = {
    locals: {
        posts:[{
                created:'today',
                tags:'1 2 3'
          }]    
    }
};

jade.renderFile(__dirname + '/each.jade', options, function(err, html){
    if (err) throw err;
    console.log(html);
});

Just make sure you are passing in a tags variable to the local variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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