jeremytripp jeremytripp - 9 months ago 133
Jade Question

Pug Conditional to Include All Subdirectories of a File Path

Simple problem that I must be overcomplicating...

I want to include a script file for any URL that contains

. Problem is, my editor URL will always be suffixed with a post title like this:
, therefore my Pug conditional never evaluates to true.

In other words, I'm looking for a conditional statement that will be true in both the following example conditions:


I have access to the path url variable already, and my conditionals are working fine when it's an exact match, so I'm trying to extend that to suffixed subdirectories. Here's what I'm starting with:

if path === '/edit-post'
//my script file

Is there maybe a way to include a regex expression or possibly some sort of "contains" statement in Pug?

I was thinking something like
. Also got desperate and tried using
which all you folks smarter than me will already know doesn't work in Pug templates!

Maybe there's a better way to achieve this altogether? Searched for hours. Tried a hundred combos. Pug's documentation is sparse. Plz send help! :)

Side note: I did find this neat little Pug/Jade conditional doc I've been using as a tester. Might be of use.

Thanks in advance!

Answer Source

Actually I recommend You:

1) to rethink Your routing

2) create /layouts/admin for admin panel layout

3) create /admin/posts/edit view for edit purposes

edit example

But for Your question here is straightforward example:

router.get('/post/:slug/edit', (req, res) => {
    .findOne({slug: req.params.slug})
    .exec((err, post) => {
      res.render('post', {includeEdit: true, post});
  • Post is mongoose model

and in Your view file:

if includeEdit === true
  include partials/edit