pug

Express+jade: local variable not available in view

不羁的心 提交于 2019-12-05 11:45:46
问题 I ran into a very basic problem but I can't seem to find the answer to it. I am working with node.js , express and I am just trying to pass a local variable into the view like this: app.get('/', function(req, res){ res.render("index", {locals: { title: "Blog", } }); }); My index view is equally simple: h1= title But for some reason, I keep getting this error as if the local variable is never passed: 500 ReferenceError: /home/spartan/Node_Projects/test/views/index.jade:1 > 1| h1= title 2|

How to generate a pure JavaScript file with Jade

空扰寡人 提交于 2019-12-05 11:15:41
I know that Jade is for producing HTML instead of JavaScript, but for a project I'm working, it would be great, and a huge time saver, if I could do this without adding a pipe in every line: | (function(){ | //Some JavaScript Code | var foo="#{bar}"; | console.log("This a JavaScript file generated with Jade"); | })() The idea is to use the output of this template as the source of a regular JavaScript include like this: <script type="application/javascript" src="foo.generated.js"></script> So doing something like this: script(type="application/javascript"). (function(){ //Some JavaScript Code

Global variable for Jade templates in node.js

人盡茶涼 提交于 2019-12-05 09:55:59
I am using node.js with Jade templating system. Assume, I have these routing rules: // ./routes/first.js exports.first = function(req, res) { res.render('first', { author: 'Edward', title: 'First page' }); }; // ./routes/second.js exports.second = function(req, res) { res.render('second', { author: 'Edward', title: 'Second page' }); }; And these dummy views: // ./views/first.jade html head title #{author} – #{title} body span First page content // ./views/second.jade html head title #{author} – #{title} body span Second page content How can I declare author variable generally, for both views?

What does != do in jade/pug?

无人久伴 提交于 2019-12-05 09:29:16
How does != work in jade code below.. != messages() extends layout block content .spacer .container .row .col-lg-8.col-lg-offset-2.col-md-10.col-md-offset-1 a(class='btn btn-tiny btn-primary' href='/manage/categories/add') Create Category h1= title small a(href='/manage/articles') Manage Articles != messages() table(class='table table-striped') tr th Category Title th each category, i in categories tr td #{category.title} td a(class="btn btn-tiny btn-default" href="/manage/categories/edit/#{category._id}") Edit app.js app.use(require('connect-flash')()); app.use(function (req, res, next) { res

IE conditional statements in Jade template engine

大城市里の小女人 提交于 2019-12-05 08:04:53
How can I convert following IE conditional statements in JADE language : <!--[if IE 8]> <html lang="en" class="ie8"> <![endif]--> <!--[if IE 9]> <html lang="en" class="ie9"> <![endif]--> <!--[if !IE]><!--> <html lang="en"> <!--<![endif]--> I have tried following but it is not working: //if IE 8 html lang="en" class="ie8" //if IE 9 html lang="en" class="ie9" //if !IE html lang="en" // <![endif] It is showing following output : <!--if IE 8html lang="en" class="ie8" --> <!--if IE 9html lang="en" class="ie9" --> <!--if !IEhtml lang="en" --> <!-- <![endif]--> Can some one guide me how it can be

How to iterate and parse JSON data in Node/Jade/Express views?

£可爱£侵袭症+ 提交于 2019-12-05 07:41:55
问题 I am very new to Node and still in the beginning stages of understanding its use and power. I apologize if this question comes across as very simple or naive - I did due research before asking. In my express js application, I sent a GET request to a remote API and properly received the appropriate JSON data in return. I then passed it to my views (Jade) and currently have the ability the print the JSON in string form. That all works properly. The JSON data I have is a group of people/members

Using javascript code in Jade views - if(variable) shows undefined instead of passing

半城伤御伤魂 提交于 2019-12-05 06:42:15
So this is a recurring issue I have and haven't found another example on SO so here goes: When rendering Jade templates I get 'variableName' undefined even when using -if(variableName) in the template. Example (I'm using this as a partial for 'info' flash messages): -if(info) - if(info.length){ ul -info.forEach(function(info){ li= info -}) -} This returns 'info' is not defined instead of not rendering anything when there isn't a flash/info message. Does anyone know what I'm doing wrong? I'm aware of the typeof(variable) != 'undefined option as mentioned. If I wanted to do something like -if

Unable to access req.user with Passport.js and Express 4

99封情书 提交于 2019-12-05 06:13:31
I've been creating an app with Passport, Express 4 and Jade. I would like to show the user a navbar that changes when they log in. However, I cannot access req.user for any other page than the profile page, which calls isLoggedIn: function isLoggedIn(req, res, next) { // if user is authenticated in the session, carry on if (req.isAuthenticated()) return next() // if they aren't redirect them to the home page res.redirect("/login") } Using any other function to not redirect the user when not logged in results in req.user being undefined . I get the user like this: router.get("/profile",

How to debug print an object from jade

萝らか妹 提交于 2019-12-05 04:33:43
How to debug print an object from jade,Like console.log() in javascript You can debug with console.log from jade like this: div - console.log(the_object_you_want_to_log) div This helped me! Able to see whole tree of Object in console. Start script. from leftmost and just give space or tab according to your template before console.log. script. console.log(!{JSON.stringify(Object)}) You can debug like this div = your_debug_data div or formatted style like: code.formattedDebug #{your_debug_data} 来源: https://stackoverflow.com/questions/32021040/how-to-debug-print-an-object-from-jade

Expanding an object to set attributes in Jade

大兔子大兔子 提交于 2019-12-05 04:20:34
I would like to be able to pass in an object with key/value pairs that represent attributes for an element. Is this possible with Jade? Any solution that allows me to pass an attributes collection into my template would be sufficient, but the ability to mix explicitly declared attributes with attributes extracted from an object (as below) would be ideal. The following syntax does not work, it is just an example of what I'd like to do. For example, if I passed this: { name:'username', value:'bob', attributes: { maxlength: 16 } } To this template: input(name=name, value=value, attributes) The