How do I pass variables across Jade template blocks in Express 3?

407 views
Skip to first unread message

Aleksey Smolenchuk

unread,
Sep 12, 2012, 9:29:14 PM9/12/12
to jad...@googlegroups.com
I am running Express 3.0 with Jade template engine. I'm trying to pass a variable from a template page to the main layout page. Like this:

layout.jade (snippet):


!!! 5
html(lang='en')
    head
        meta(charset='utf-8')
        title= locals.title
      
        - if (typeof(stylesheets) !== 'undefined')
            each stylesheet in stylesheets
                link(rel='stylesheet', href='/stylesheet/#{stylesheet}.css')

    body
        block body
           

           

landing.jade (snippet):

stylesheets = ["landing"]
extends layout
   
block body
    h1 Test 1234


The generated code does not include the extra stylesheet tag. I tried `- var stylehseets...` and renaming 'stylesheets' fearing it might be a keyword.

Any suggestions?

Jan Buschtöns

unread,
Sep 13, 2012, 1:19:35 AM9/13/12
to jad...@googlegroups.com
That's a general issue
I came across too. I solved it with blogs. (block styles in your
layout and append styles for each included page, same for scripts and
other resources)

I don't like this approach, because defining the same <link> structure
over and over again isn't really DRY. But it's still better then
passing the Array as a local. We could make it DRY by defining a
style(href) mixin at the layout level, so that in append styles, we'd
only have to style(/css/style.css).

How do you guys solve this?

Aleksey Smolenchuk

unread,
Sep 13, 2012, 1:28:46 AM9/13/12
to jad...@googlegroups.com
The mixins seems like a good idea!

Set up 'block stylesheets' in the head of layout.jade. Then do

block stylesheets
    mixin stylesheet('style1')
    mixin stylesheet('style2')

in the template part files. This is a pretty good solution actually. Thanks!

Jan Buschtöns

unread,
Sep 13, 2012, 2:38:56 AM9/13/12
to jad...@googlegroups.com
I'd recommend appending to the block with block append stylesheets or
just append stylesheets, cause this way you won't overwrite other
stylesheets that already have been added to the block.
Reply all
Reply to author
Forward
0 new messages