Shristi Sharma Shristi Sharma - 4 months ago 16
Jade Question

Don't Want Include Tag's Stylesheet to Mix With Current Page's

I have an include tag in dashboard.jade to include my header.jade file, but the header.jade file has its own stylesheet and when I run it, dashboard.jade's stylesheet also applies to the included header.jade. Is there a way to have dashboard.jade's stylesheet to apply only to it and not the included header.jade?

Here's my code for dashboard.jade:



doctype html
html
head
title Todo List
| 	
|
link(rel='stylesheet', type='text/css', href='assets/css/todos.css')
| 	
|
link(href='https://fonts.googleapis.com/css?family=Roboto:400,700,500', rel='stylesheet', type='text/css')
| 	
|
link(rel='stylesheet', type='text/css', href=' https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css')
| 	
|
link(rel='stylesheet', href='//cdnjs.cloudflare.com/ajax/libs/lemonade/2.1.0/lemonade.min.css')
| 	
|
script(type='text/javascript', src='assets/plugins/jquery-3.0.0.min.js')
|
|
body
include partials/header





And here's my code for header.jade:



doctype html
html
head
title Eisenhower Productivity Tool
// Meta
meta(charset='utf-8')
|
meta(http-equiv='X-UA-Compatible', content='IE=edge')
|
meta(name='viewport', content='width=device-width, initial-scale=1.0')
|
meta(name='description', content='')
|
meta(name='author', content='')
|
|
link(rel='shortcut icon', href='../favicon.ico')
|
|
link(href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800', rel='stylesheet', type='text/css')
// Global CSS
link(href='../assets/plugins/bootstrap/css/bootstrap.min.css', rel='stylesheet')
// Plugins CSS
link(rel='stylesheet', href='../assets/plugins/font-awesome/css/font-awesome.css')
// Theme CSS
link#theme-style(rel='stylesheet', href='assets/css/styles.css')





Any help is greatly appreciate, thanks!

Answer

To use different CSS-Stylesheets I recommend to use a layoutbased enviroment with typical Jade/Pug block statements.

This is how a layoutbased folder structur can look like:

|--./
|-- |--jade
|-- |-- |-- layouts
|-- |-- |-- |-- mylayout.jade
|-- |-- |-- template_1.jade
|-- |-- |-- template_2.jade

This could be your layout file mylayout.jade:

doctype html

block vars
  // Some default variables

html

  head
    block head
      // default head for title and meta

    block defaultCSS
      // default css
      link(rel='stylesheet', type='text/css', href='path/to/default_style.css')
      style.
        body {}

   block additionalCSS

  body
    block body
      // default html in body
    block footer

  block defaultJS
    script.
      var someDefaltJavaScript = 'awsome"

And here the template files

template_1.jade:

extends layout/mylayout.pug

block head
  // this overrides the default "block head" from the layout
  // So put your special meta for your page here

block additionalCSS
  link(rel='stylesheet', type='text/css', href='path/to/other_style.css')

block body
  .this
    .is
      #where
       .your.content.goes

template_2.jade:

extends layout/mylayout.pug

block head
  // this overrides the default "block head" from the layout
  // So put your special meta for your page here

//- We dont use the "block additionalCSS" because we dont need it in this template

block body
  .this
    .is
      #where
       .your.content.goes

Beware to compile only the templatefiles, here is some other example: http://jade-lang.com/reference/extends/.

You can also take a look into this small jade app of mine on github: https://github.com/pure180/gulp-pug-inheritance-test/tree/master/app

Comments