the Includes
We are going to be using includes in our process, so we can demonstrate how to use them. Remember the includes will go in the views/includes sub-directory.
Now in our original Bodingles html app, which renders on the client-side, we used external, header/footer Js files that are using innerHTML to build and display the header and footer. In the individual html pages that use the header/footer, we simply add an empty <div> with an id that we access in the Js file. And of course we need the <script> elements before our closing <body> elements So in any of our html pages, we'll have:
- html
- <body>
- <div id="header"></div>
- ...
- <div id="footer"></div>
-
- <script src="header.js"></script>
- <script src="footer.js"></script>
- </body>
Now in our header/footer Js files, we'd have:
- JavaScript
- const head = document.querySelector('#header');
- head.innerHTML = 'All header HTML code goes here';
- const foot = document.querySelector('#footer');
- foot.innerHTML = 'All footer HTML code goes here';
This works but remember, all of these files are rendered on the client-side.
We want to incorporate the same general process of having external files that we can incorporate into other files, but we want to do this while rendering everything, on the server-side. As it turns out, we can utilize essentially the same process with includes, but done a little differently.
Remember that server-side rendering can be done with html files, but with limitations. These file can't access any external links such as css or Js.
These files are normally written with html coding, but saved as ejs files. This is done so that we can "embed" Js coding within our html coding, which is how our includes works. They are Js coding written within an html page.
One last note about our includes files here. We do NOT need to create a complete HTML page with all the essential elements such a <head> and <body> elements. We ONLY need the HTML coding for the specific data that we want in our include.
So, for example, the code for our header/footer.ejs include files will look something like this:
- HTML/EJS
- <header>
- header html coding goes here
- </header>
- or
- <footer>
- Footer html coding goes here
- </footer>
Remember, we save these files as ejs files (header.ejs/footer.ejs).
And finally, we can "include" our includes files, into our other ejs files. Remember, they MUST be ejs files so that we can embed the Js coding within our html coding. The include call is one line of Js code, written within some html coding, using the ejs syntax, like so:
- HTML/EJS
- <body>
- <%- include('includes/header') %>
- ...
- ... other page coding here ...
- ...
- <%- include('includes/footer') %>
- </body>
Very simple coding here to include our external header/footer.ejs files. A couple of things to note here:
- Any external "includes"" files can be either html or ejs files, however
- if there is any "scripting" or js coding involved, they must be ejs files
- Any pages that want to include the external "include" files must be ejs files.
- And finally, we must use <%- as the ejs syntax to generate the include.
So we should be able to create our include files, save them in the includes sub-directory, and actually include them into any ejs file. We can now set up our individual pages (views).