How to include / extend the jade file from the root of my templates directory in express?

Given the sample structure:

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

Is there a way to extend layout.jade in example.jade without having to track the number of parent directories?

This works as intended:

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

But it would be preferable to do something like:

 extends /layout 

Or even the alias / hardlink and use it like:

 extends layout 

Also assuming that someone knows the solution, can it also be applied for inclusion?

+6
source share
2 answers

There is a change that has been introduced in this particular commit .

Basically, if the first character found in the include declaration is a slash and is determined based on, it will look for a pattern in that path.

You must configure Jade first:

 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 deep it is, you can:

 include /layout 
+1
source

Looking at the code itself, I realized that someone cannot just refer to the jade file related to the root of the patterns.

However, a simple approach is to patch jade / lib / parser.js, replacing two instances:

 , dir = dirname(this.filename); 

FROM

 , dir = path.indexOf('/') ? dirname(this.filename) : this.options.settings.views; 

I could also warn you that this is not sufficiently tested and is not intended for use in production.

0
source

Source: https://habr.com/ru/post/927794/


All Articles