Check me out at mjsaldanha.com :D
We are using EJS for making HTML templates, so we have the ability to embed javascript within HTML, such as below.
<h2>
Hello, <% callname %>!
</h2>
And to render such page, we would need to call res.render("page", { callname: user.callname });
.
Consider that we have a navigation bar that shows the name of the user whenever he is logged in.
The navigation bar appears in every page, so we would need to render every page by passing user.callname
to the rendering call, which is bothersome and prone to errors.
That's why we decided to encapsulate the rendering within a class called Renderer, and all of these global template parameters, such as user.callname
,
are passed to the renderer upon its construction (see below).
class Renderer {
constructor(callname = null, specialUser = false, language = "en"){
this.callname = callname;
this.specialUser = specialUser;
this.language = language;
}
...
}
The Renderer object is created by a middleware and placed in req.renderer
.
Then, for rendering a page, we should always render it through calls to the Renderer methods, which always pass the global template parameters to the EJS renderer.
For making the website available in multiple languages, we face a difficulty: cookies are extremely easy to use, but Search Engine Crawlers won't deal well with it. So, how did we decide to solve this problem?
Our home page contains the main portion of our website, so it isn't a big loss that the crawler can see the other pages in, say, japanese. Besides, the other options for localizing the website would be:
mjsaldanha.com/ja/articles/
mjsaldanha.com/articles/?set_lang=ja
ja.mjsaldanha.com
And each of these options are a gigantic pain in the ass because we'd have to change all anchors <a href="https://github.com/matheushjs/mjsaldanha.com/blob/master/.." ></a>
within the website to point to the language currently being used by the user.
By using cookies we avoid this problem, and manage to save the user's preference.