The Web Developer Bootcamp 2024

Colt Steele

Back to Templating Index Page


 

Express Js
Includes

Sometimes you may find that you are writing the same code for several different pages. A "Header" and a "Footer" are good examples of this as you will probably want these on most of your site pages. Say, you have a sight, you want to change some of the "Header" or Footer" data. If you only have a few pages it becomes a nuisance, but if you have a few hundred pages, it becomes very problematic.

*note: some programmers call these files "partials"", and some call them "includes".

A better way to do this would be to create these files as separate files, and then include them into our pages, where needed. Like PHP, Express gives us a way to include separate files into any different location within a webpage coding. The caveat' is that they must be ejs files.

The way to set this up is pretty simple. First you need to create a sub-directory to hold our include files. You can name this sub-folder anything you like (I normally call it includes). So Our directory structure would look something like this:

  • Folder Icon  express
  • Folder Icon  node_modules
  • Folder Icon  views
    • Folder Icon  includes
    • ejs Icon  projectFile1.ejs
    • ejs Icon  projectFile2.ejs
  • ejs Icon  index.js
  • ejs Icon  package-lock.json
  • ejs Icon  package.json

You then just need write your include HTML files as ejs files.

Remember, with ejs files, you can incorporate both HTML, and Js as needed. Also, the base structure of a ejs file is the same as for an HTML file, but you don't write an entire HTML page with the html declarations, head section, body, and closing body and html tags. These are already incorporated in the file where you are placing your includes.


Creating the Include File

To write our include files, we just need to write the specific code we need. For example:

  • <p>Some Header Code goes here.</p>
and save the file, as an ejs file. Make sure to save this file in the folder we created for our include files. And if we also create a "footer" file, our directory structure would look like this:
  • Folder Icon  express
  • Folder Icon  node_modules
  • Folder Icon  views
    • Folder Icon  includes
      • ejs Icon  header.ejs
      • ejs Icon  footer.ejs
    • ejs Icon  projectFile1.ejs
    • ejs Icon  projectFile2.ejs
  • ejs Icon  index.js
  • ejs Icon  package-lock.json
  • ejs Icon  package.json

An now all we need to do is to actually include these files into any of our project files. If we recall, in our index.js file, we designated our default views directory. So all we need to do is to reference the path to our includes/file directory, like so:

  • <%- include('includes/header') %>
Not that we are using the Eetended JavaScript syntax (ejs) <%- which "Outputs the unescaped value into the template". So what we get is the header.ejs coding included wherever we place the code within our page. More information can be found here under the Includes section.


Back to Top