Tips for sending a variable from Express to a Jade template

I have searched for answers on this topic, but I haven't found one that addresses the entire process. I am feeling quite stuck.

Below is my code:

express code

res.render('index', {expressVar:"My Example"});


template.jade

doctype html

block vars

html
    head

    body
        | #{working}

        | #{notWorking}

        | #{notWorking2}


index.jade

extends template.jade

- var localVar = "Another Example"

block vars
    - var working     = "This works"
    - var notWorking  = expressVar
    - var notWorking2 = localVar


I am attempting to print both of the variables that are currently not working inside the template.

Thank you in advance.

Answer №1

David Lee authored a comprehensive blog post guiding readers on how to unravel this dilemma:

In order to solve this problem, it is essential to enclose all markup within a root block and define variables in the template's block. These variables will then be accessible to any child scopes within that scope.

template.jade

doctype html

block page
    html
        head

        body
            | #{active}

            | #{inactive1}

            | #{inactive2}

index.jade

extends template

- localVar = "Another Illustration"

prepend page
    - active     = "This is functional"
    - inactive1  = expressVariable
    - inactive2  = localVar

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Node.js and the Jade templating engine, display the value of a passed variable

Asking such a basic question makes me feel guilty. app.get('/skumanagement/:id', function (req, res){ var options = req.params.id; // req.params.id = itemidx database.skuGetDetail(options, function (error, data){ winston.log('inf ...

Steps to include a registry in npm installation for a scoped package

I am attempting to use npm install with a registry and scope. An example would be @test:registry=url After trying various methods, such as: npm install --registry @test url npm install --registry@test url I have not been successful in making it work. ...

I am curious as to why Helmet is preventing access to YouTube videos in my Express application that utilizes an embedded YouTube player

In developing my Express app, I allowed users to post YouTube videos embedded with iframe elements in the relevant view using the YouTube embedded player. However, upon attempting to deploy the app, I encountered an issue after adding Helmet with its recom ...

Sorry, but the credential implementation passed to initializeApp() through the "credential" property was unable to retrieve a valid Google OAuth2 access token

const admin = require('firebase-admin'); require('dotenv').config() // const serviceAccount = require("../service-account-file.json"); const check = process.env.GOOGLE_APPLICATION_CREDENTIALS; //GOOGLE_APPLICATION_CREDEN ...

Encountering TypeScript errors when trying to reference Angular2 within a Gulp setup

The issue at hand is: [11:16:06] TypeScript: 103 semantic errors [11:16:06] TypeScript: emit succeeded (with errors) I am currently using node v5.7.0 and npm 3.6.0 gulp -v: [11:26:58] Requiring external module babel-register [11:26:58] CLI version 3.9 ...

Having trouble receiving a response from the websocket using RxJS in Node.js with Angular

Currently experimenting with setting up a WebSocket using rxjs webSocket I have managed to create a node server without any errors, but it is not sending messages to the server or connected users When I switch to 'ws://echo.websocket.org/', I c ...

Having trouble installing Phantomjs on Windows using npm?

Currently, I am attempting to install phantom.js while at work. Although I have downloaded the source code and installed the .exe file to access the phantom shell, it still remains uninstalled. Every time I try running commands like node ./install.js or np ...

Ways to decouple business logic from controllers within Express.js applications

I recently faced a question in an interview about separating business logic from controllers in Express. Do you believe this solution is the correct approach? const db =require('./db') const helpers =require('./helpers') exports.getBo ...

Is there a way to assign a role to a user without requiring them to send a message beforehand?

I've been searching for a solution to this issue, but all I could find were instructions on how to assign a server role to someone who has interacted in some way. Is there a way to locate a specific user on a server and assign a role to them without ...

Running `husky install` results in a path error

I am facing an issue while trying to manually include Husky in my project following the instructions provided here: After executing npm install husky --save-dev, Husky is successfully added and installed as a dev dependency. However, when I attempt to run ...

Should we integrate sailsjs with reactjs or reactjs with sailsjs? What steps can be taken to achieve this integration?

Currently, I am using SailsJS as my web API and ReactJS for the frontend. Can someone please guide me on how to integrate one into the other seamlessly? I am a beginner in this field, so feel free to highlight any mistakes I may have made. ...

Discord.js version 14 is throwing a TypeError that says it cannot read properties of undefined, specifically 'commands'

Lately, I've been working on a discord.js bot to refresh my JavaScript skills after a long break. I've been following a tutorial on reloading slash commands and events, but I encountered this error: TypeError: Cannot read properties of undefined ...

Using JSON data in Express and invoking the next function

Is it possible to use Express 4 to send a JSON response to the front-end indicating an error and also call next(err) within the middleware, allowing the server to handle the error as well? Or are these actions completely separate? Based on my current unde ...

Tips on escaping a loop in node.js?

I have a code written in node.js that verifies username and password from a JSON of users. One issue I'm encountering is that the loop keeps iterating through all users even after a valid user has been found. How can I modify the code to break the loo ...

Yet another error encountered: "Headers cannot be set after they have already been sent to the client" when submitting the form

Whenever I try to submit text to a single-field form on my node.js server, I encounter the following error: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:485:11) ...

The Node.js application is having trouble connecting to the Angular application on port 3000 when the Angular app is hosted on a remote machine

I'm facing an issue with my Nodejs app not being able to connect with the Angular app on port 3000 when the Angular app is hosted on a remote machine. However, everything works fine when both apps (Nodejs and Angular) are hosted on the same machine. C ...

The installation process for npm or yarn commands is dragging on for an extended period of time on my Ubuntu system

After installing Ubuntu, I encountered slow performance when running the command to install project dependencies (npm install / yarn). It took a whole 30 minutes to complete. Any ideas on what could be causing this issue? ...

Exploring the ins and outs of leveraging the VueCompositionAPI in Vue 3

Following an update of all packages in my project and upgrading to vue 3, I encountered an issue in my code. I am unable to use Vue.use(VueCompositionAPI);. In the previous version of the project, I called it using import VueCompositionAPI from '@vue/ ...

Conducting testing sessions for middleware in Express

I am struggling to test middleware functions in my Express application using supertest and nock. The issue arises from the fact that the routes I have configured are checked by a previous middleware function to ensure the existence of a session property on ...

Module 'bcryptjs' could not be located

Recently, I added the @types/bcryptjs package to my Node.js project. Initially, there were no issues with importing it. However, when I attempted to use it in my code by including the line: console.log(bcrypt.hashSync(req.body.password)) I encountered an ...