Marteiro
Marteiro

Reputation: 175

How to pass a variable from Express to a Jade template

I've already seen a bunch of answers about this but none that covers the whole process. I'm really out of ideas.

Here is my code:

express code

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


template.jade

doctype html

block vars

html
    head

    body
        | #{working}

        | #{notWorking}

        | #{notWorking2}


index.jade

extends template.jade

- var localVar = "Other Example"

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


What i'really trying to do is to get both variables that are not working to get printed inside the template.

Thanks in advance.

Upvotes: 4

Views: 842

Answers (1)

tcanz
tcanz

Reputation: 46

Darren Schnare wrote an article that explains how to figure this out: http://darrenschnare.com/blog/2013/08/14/sharing-variables-with-jade-templates/

You need to place all markup in a root block then declare variables in this block in the template. All variables will be accessible to each scope that is a child of the the scope.

template.jade

doctype html

block page
    html
        head

        body
            | #{working}

            | #{notWorking}

            | #{notWorking2}

index.jade

extends template

- localVar = "Other Example"

prepend page
    - working     = "This is working"
    - notWorking  = expressVar
    - notWorking2 = localVar

Upvotes: 3

Related Questions