How to embed content in jade - node.js

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

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.

Multiple extend blocks

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

jade extends layout and if statement

hi and sorry for my english.
I've a very simple "layout.jade"
!!! 5
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body
block content
and a very simple "index.jade"
extends layout
block content
- if (session) {
p Welcome
- } else {
p Login
-}
With this code i've got a blank page.
Now if i remove extends layout from my "index.jade" and i put the code directly in "index.jade" everything works fine, that is to say my conditional statement give me <p>Welcome</p> or <p>Login</p> no blank page any more.
I'm new to nodejs and my problem may be evident for many of you ;)
Thanks!
It looks like you have a leading whitespace mismatch, which is a no-no in significant whitespace languages like jade. Also if and else are directly supported in jade:
extends layout
block content
if session
p Welcome
else
p Login

Resources