jeremytripp
jeremytripp

Reputation: 1038

Pug Conditional to Include All Subdirectories of a File Path

I want to include a script file for any URL that contains /edit-post. Problem is, my editor URL will always be suffixed with a post title like this: /edit-post/some-post-title, 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:

/edit-post/some-post-title
/edit-post/an-even-better-post-title

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 "/edit-post.*" or "edit-post[s/S]". Also got desperate and tried using .indexOf() 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.

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

Upvotes: 0

Views: 1454

Answers (1)

num8er
num8er

Reputation: 19372

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) => {
  Post  
    .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

Upvotes: 2

Related Questions