1

I'm getting started with node.js and particularly express.js.

Given that I like to be as D.R.Y. as possible, I've decided to have only one index.pug-template which has single HTML element main.content in the body. The actual content is created by pug.renderFile(), based on excerpt, stored in <name>.pug file.

The whole picture is this:

SERVER.JS

response.render("index", {
    content: pug.renderFile(excerpt("aboutus"), {
        title: "About Us",
        subtitle: "Some subtitle here",
        article: "Some text here"
    })
});

ABOUTUS.PUG

h1= title
h3= subtitle
article= article

INDEX.PUG

//- html-head-body routine
main.content= content

The problem is that pug.renderFile() returns HTML code in form of string and, instead of getting three elements inside main.content element, I'm getting this exact string as .innerHTML of this element.

So, what is my mistake here?

1
  • Where are you getting excerpt? Commented Jan 13, 2017 at 0:00

1 Answer 1

1

That's horrible practice. You need to use extends. Not make two calls to pug. You're baking presentation into the wrong layer. This is going to sting you later.

Not to mention, you're forcing HTML into an intermediary string and making extra functions calls that can't be optimized away.

SERVER.JS

response.render("aboutus", {
  title: "About Us",
  subtitle: "Some subtitle here",
  article: "Some text here"
});

ABOUTUS.PUG

extends index

block content
  h1= title
  h3= subtitle
  article= article

INDEX.PUG

// html-head-body routine
main.content= content

If this doesn't make sense think about how your system is going to scale. Let's say you have one pug file for a navigation bar, one for a footer and one for a header etc. etc. You're going to string them all together and pass them into your index.pug on every request?

  1. That will be very much anti-DRY.
  2. That will be slow.
  3. That will be memory intensive.
  4. That will be a maintenance nightmare.

Follow the convention it will do you well.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, that is extremely convenient. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.