Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Build a Simple Dynamic Site with Node.js Creating a Basic Template Engine in Node.js Making Our Views DRY

Jess Hines
Jess Hines
5,411 Points

Would you really have an HTML file with invalid HTML?

Regarding the footer.html file that has just two closing tags, and the header file that also isn't valid. I ask because I've never seen this type of thing before, and makes me think that the next lessons are going to just use string concatenation rather than inserting templates into the right places.

I'm going through these courses not just how to learn to do one thing, but to observe and learn best practices for doing things as well. Of course, I know it was said that this course wouldn't use any frameworks, and that's fine, but from other things I've learned elsewhere about templating, they all use a nesting structure, not a concatenation structure.

2 Answers

I personally find include files for templates to be confusing and harder to troubleshoot if any include files are just HTML closing tags. I prefer include files to be valid HTML blocks even if that means a lot of the templates that include them have extra / repetitive markup.

It isn't wrong to have include files with HTML closing tags, just more processing and remembering required by the developer working with said files. So yeah, not wrong, just sub-optimal. ;)

Jess Hines
Jess Hines
5,411 Points

Yeah, to me, a template should be self-contained. It will make maintenance harder if I have to, in one template, remember what the markup was in a parent template, so that I close out the tags right.

I also think that HTML templates should validate on their own. Of course, many of the templating languages do this right, and templates are injected or included in others, and are modular. My question here was basically just, "If you're going to introduce something like templating, why not do it in a way a real developer would?" And in a course, it shouldn't just be about, here's one way you could this, but don't really do it that way; it should reinforce or follow established best practices, no matter what the subject matter.

These template partials cannot be accessed by the user individually, so they will never be rendered on their own. Thus they are always valid when rendered with a header and body template. In templating engines this makes sense because because usually the filetypes are not .html, for example EJS uses .ejs. If you're worried about your devs not knowing these are only valid html following another template then you can leave a comment. Normally the header and footer would be included with every request so your devs won't be manually including them every time they make a new route & template.

If you were to make it valid html on its own, then it would be invalid html when rendered by the server and sent to the client.