Multiple extend blocks - node.js

So I'm using jade to build a couple of webpages, but my landingpage differs in the other pages in the way that it hasn't got a menu. Except for that it shares the same header and footer. My problem is that I'd like to conditionally include menu.jade into conversion to html. I'd like to do something like this:
base.jade:
doctype html
head
title= title
body
block menu
block content
include footer
menu.jade
extends base
ul
li Home
li Contact
index.jade:
extends base
block content
p hello landingpage
page.jade
extends base
block content
p hello subpage
The thing is: when I convert index.jade everything is fine; my pages looks like I want it to. But when I convert page.jade I somehow would like to include menu.jade as well,o I don't have to rewrite my menu across all subpages.

menu.jade
ul
li Home
li Contact
page.jade
extends base
block menu
include menu
block content
p hello subpage
anotherpage.jade
extends page
block content
p hello another page

Related

Express 4+pug disable layout

I want to ajax for blocks that without layout
Such as
extends layout
block content
p xxxx
I want to get the content in block
if !layout
include layout
The solution
If you don't want the standard pug layout then create a file that doesn't have extends layout in it:
doctype html
html(lang='en-us')
head
title Page Title
body
div Content goes here
You don't need to do anything in your app.js file to make this happen. You've probably figured out that none of the statements in your question work. This is all controlled in the pug template file.

pug - create a layout for header including login / logout links

In my Express JS web app, I need to create a layout view that is attached to every other view as included. The layout is supposed to contain a header and a login or logout link depending user authentication condition.
layout.pug:
doctype html
html
head
title= title
link(rel='stylesheet', href='/bootstrap/css/bootstrap.css')
link(rel='stylesheet', href='/stylesheets/style.css')
script(src='/jquery/jquery.min.js')
script(src='/bootstrap/js/bootstrap.min.js')
block extraHeader
block extraScript
body
block content
home.pug that contains the layout on the top which may contain a log out link if user was already logged in.
extends layout
block content
h1= title
p Welcome to #{title}
How can I generate the header in layout.pug?
You need to add the code in between the body tag and block content:
body
div Header goes here
block content
You can use conditional logic to control a button in there too:
body
div Header goes here
if(user)
button(onclick='logout()') Logout
else
button(onclick='login()') Login
block content
In one line using a ternary operator:
body
div Header goes here
button(onclick= user ? "logout()" : "login()")= user ? "Logout" : "Login"
block content
Of course, this assumes that your route passes a variable named "user" in the render function.

I am extending a pug layout(parent) to another pug layout(child).I am getting an error 'unexpected token:indent' from the parent layout

This file name is layout.pug(parent):
doctype html
html
head
title knowledgebase
body
block content
br
hr
footer
p Copyright © 2018
And this file name is index.pug(child):
extends layout
block content
h1 articles
h1 #{title}
At first I thought everything was good with your code, then I looked closely at what was pasted here.
If inspect the code in layout.pug you'll see that there's a leading space on every line. Make sure that doctype html and html (which are your root elements in the parent template) have ZERO spaces at the start of the line.
Otherwise your code looks good and should work as expected.

Duplicate navigation when using "includes" with Jade/Node

I have a one page website that I would like to keep but I also need individual sections to be viewed as independent pages. I'm using Jade with Node and everything is working fine until I start including files/blocks.
base.jade
!!! 5
head
body
.nav
// menu
block content
index.jade
extends layout/base
block content
p
| This is the index page.
include about
about.jade
extends layout/base
block content
p
| This is the about page.
What I'm trying to do is use the index page to display all of my pages and then have the individual pages extend the base.jade file. What's happening is I'm getting multiple navigation bars on index.jade because I'm extending base.jade twice. I'm a little stuck and feel like I'm missing something simple here.
Any help is appreciated, thank you.
the file you include should only have the content portion such as
about-content.jade
p
| This is the something about the site.
then index.jade is
extends layout/base
block content
p
| This is the index page.
include about-content
and about.jade
extends layout/base
block content
p
| This is the about page.
include about-content

How to embed content in jade

I'm currently experimenting with jade template engine. I've got the follow basic code in my layout.jade file:
!!! 5
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body!= body
header != partial('partials/head')
'dependant page content to go here'
footer != partial('partials/foot')
I'd like every page to follow this same structure (for now). However I would like the content to change, and the content should be depended on what is inside the 'pagename'.jade files e.g. index.jade:
section#page-content
h1= title
p Welcome to #{title}
What i'm trying to say is, upon a new page loading.. the content tag in layout should somehow be replaced by the appropriate tag of the page being loaded.
I think you're looking for:
body
header
p my header
section!= body
footer
p my footer
In this case, section!= body is replaced by whatever is in the 'pagename'.jade files.
use blocks
in layout.jade:
body
block some-block
p Blah
in test.jade
extends layaout
block some-block
p What

Resources