Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I include/extend a jade file from the root of my template directory in express?

Given a sample structure:

/layout.jade
/really/deep/dir/inside/my/template/folder/example.jade

Is there any way I can extend layout.jade in example.jade without having to keep track of the number of parent directories?

This works as intended tho:

extends ../../../..etc../../../layout

But it would be preferable to do something like:

extends /layout

Or even alias/hardlink and use it like:

extends layout

Also assuming somebody knows a solution, can it also be applied to include?

like image 827
Alex Tzo Avatar asked Oct 15 '12 23:10

Alex Tzo


People also ask

What is Jade file extension?

Jade is a template engine for node. js and the default rendering engine for the Express web framework. It is a new, simplified language that compiles into HTML and is extremely useful for web developers.

How do you use jade in Express?

In order to use Jade with Express. js, create sample. jade file inside views folder and write following Jade template in it. doctype html html head title Jade Page body h1 This page is produced by Jade engine p some paragraph here..

How do I open a jade file?

All jade files need to be transformed in the HTML. Also don't forget that you need to install other dependencie like express, nodemailer, etc (see requires in the source code). And the application should by available on http://localhost/3000. All Jade templates will be correctly rendered and displayed as HTML.


1 Answers

There is a change that was introduced in this particular commit.

Basically, if the first character found in the include statement is a forward slash and basedir is defined, it will look for the template in that path.

You must first setup Jade:

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.locals.basedir = app.get('views');

Then, from any template, no matter how deeply nested, you can do:

include /layout
like image 195
Pier-Luc Gendreau Avatar answered Nov 02 '22 15:11

Pier-Luc Gendreau