pugjs / pug

Pug – robust, elegant, feature rich template engine for Node.js
https://pugjs.org
21.68k stars 1.95k forks source link

sharing variables across blocks in jade #1603

Open DTrejo opened 10 years ago

DTrejo commented 10 years ago

@dschnare's great article on how to share variables in #jadelang

http://darrenschnare.com/blog/2013/08/14/sharing-variables-with-jade-templates

Could be a good addition to the docs!

Thanks Forbes for maintaining Jade! D

dschnare commented 10 years ago

Hey thanks @DTrejo, I wrote that article so far back I had forgotten about it. That technique really does come in handy for sure. You are right though, generally speaking it's a technique to share variables across blocks not just templates. I remember when I wrote the article I had only spent a few days with Jade so I hadn't realized its general utility.

jordancalder commented 9 years ago

I have been running into issues with this the last few days. Am I to understand that variables passed to a certain view, will not be available to the base layout that it extends?

If this is the case, I am seeing a problem. In my base layout file, I'd like the navbar to display user information. But it would be tedious to add a block to every single view in order to expose those variables to the layout. Is there a way to expose those user vars once globally so that the layout can use them across all views?

DTrejo commented 9 years ago

@jordancalder yes there is! Follow this example here, and put ALL your variables in the block that goes at the very top of the layout. This will expose those variables to anything that comes after them in the layout.

See this great example: http://darrenschnare.com/blog/2013/08/14/sharing-variables-with-jade-templates/

good luck! D

jordancalder commented 9 years ago

@DTrejo Thanks for the response. Maybe I'm not fully understanding, but doesn't this mean that in every view that extends the layout, I will have to add a block that passes those variables to the layout?

DTrejo commented 9 years ago

hey, yes.

It's not too much of a change, and you only need to do it when it is trying to expose variables to the WHOLE layout. e.g. if you are changing meta tags from inside a jade file that extends the layout.

Cheers, D

On Tue, Nov 11, 2014 at 9:17 AM, Jordan Calder notifications@github.com wrote:

@DTrejo https://github.com/DTrejo Thanks for the response. Maybe I'm not fully understanding, but doesn't this mean that in every view that extends the layout, I will have to add a block that passes those variables to the layout?

— Reply to this email directly or view it on GitHub https://github.com/jadejs/jade/issues/1603#issuecomment-62581508.

See my projects at dtrejo.com Read my tips for developers dtrejo.com/blog