Exploring different templating engines in JavaScript MVC

JavaScript MVC frameworks provide a way to structure and organize client-side web applications. One important aspect of these frameworks is the use of templating engines, which allow developers to separate logic and presentation in their code. In this blog post, we will explore some popular templating engines used in JavaScript MVC and discuss their features and benefits.

1. Handlebars.js

Handlebars.js is a widely-used templating engine that aims to provide a semantic and expressive syntax for creating dynamic templates. It is based on the Mustache template language but extends it with additional functionality.

Key Features:

Example code using Handlebars.js:

const template = Handlebars.compile(`
  <div>
    <h1></h1>
    <p><header>
  <div class="header-small">
    <a href="https://colinchjs.github.io">Colin.js</a>
  </div>
</header>
<div class="post">
  <div class="post-title">Implementing server-side rendering with JavaScript MVC frameworks</div>
  <span class="post-date">
    <time>23 Sep 2023</time>
  </span>
  <div class="post-tag">
    <ul>
      
      <li>
        <a href="https://colinchjs.github.io/tags#VueJS">
          <span>VueJS</span>
        </a>
      </li>
      
      
      <li>
        <a href="https://colinchjs.github.io/tags#ReactJS">
          <span>ReactJS</span>
        </a>
      </li>
      
      
    </ul>
  </div>

  <p>In this blog post, we will explore how to implement server-side rendering with popular JavaScript MVC frameworks, focusing on Vue.js and React.js.</p>

<h2 id="server-side-rendering-with-vuejs">Server-side rendering with Vue.js</h2>
<p>Vue.js provides built-in support for server-side rendering through its official package, Vue SSR. To implement server-side rendering with Vue.js, follow these steps:</p>

<ol>
  <li>Set up a server: You need a server to render the Vue application on the server side. You can use Node.js or any other server-side language/framework to create an HTTP server.</li>
  <li>Use Vue SSR package: Install the <code class="language-plaintext highlighter-rouge">vue-server-renderer</code> package using npm or yarn.</li>
  <li>Create an entry file: Set up an entry file for the server-side rendering. This file will import your Vue application and generate the server-rendered HTML. You can use the <code class="language-plaintext highlighter-rouge">createRenderer</code> function provided by <code class="language-plaintext highlighter-rouge">vue-server-renderer</code> to create a renderer instance.</li>
  <li>Render the app: Use the renderer instance to render your Vue application to a string. Pass the app instance and any initial state as parameters to the <code class="language-plaintext highlighter-rouge">renderToString</code> method.</li>
  <li>Inject the rendered HTML: Once the app is rendered to a string, inject it into the HTML template of your server response before sending it to the client.</li>
</ol>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// server.js</span>
<span class="kd">const</span> <span class="nx">Vue</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">vue</span><span class="dl">'</span><span class="p">);</span>
<span class="kd">const</span> <span class="nx">renderer</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">vue-server-renderer</span><span class="dl">'</span><span class="p">).</span><span class="nx">createRenderer</span><span class="p">();</span>

<span class="c1">// Create an app instance</span>
<span class="kd">const</span> <span class="nx">app</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Vue</span><span class="p">({</span>
  <span class="na">template</span><span class="p">:</span> <span class="s2">`&lt;div&gt;Hello, SSR!&lt;/div&gt;`</span>
<span class="p">});</span>

<span class="c1">// Render the app to a string</span>
<span class="nx">renderer</span><span class="p">.</span><span class="nx">renderToString</span><span class="p">(</span><span class="nx">app</span><span class="p">,</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">html</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">console</span><span class="p">.</span><span class="nx">error</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
    <span class="c1">// Handle the error</span>
    <span class="k">return</span><span class="p">;</span>
  <span class="p">}</span>

  <span class="c1">// Inject the rendered HTML into the server response</span>
  <span class="nx">serverResponse</span><span class="p">.</span><span class="nx">send</span><span class="p">(</span><span class="s2">`&lt;html&gt;&lt;body&gt;</span><span class="p">${</span><span class="nx">html</span><span class="p">}</span><span class="s2">&lt;/body&gt;&lt;/html&gt;`</span><span class="p">);</span>
<span class="p">});</span>
</code></pre></div></div>

<h2 id="server-side-rendering-with-reactjs">Server-side rendering with React.js</h2>
<p>React.js also supports server-side rendering through its official package, Next.js. Heres how you can implement server-side rendering with React.js using Next.js:</p>

<ol>
  <li>Set up a Next.js project: Initialize a new Next.js project using the official command line tool or by manually configuring your project.</li>
  <li>Create pages: Next.js handles server-side rendering automatically for the pages inside the <code class="language-plaintext highlighter-rouge">pages</code> directory. Create your React components as separate files inside the <code class="language-plaintext highlighter-rouge">pages</code> directory.</li>
  <li>Fetch data: Use Next.js built-in <code class="language-plaintext highlighter-rouge">getServerSideProps</code> or <code class="language-plaintext highlighter-rouge">getStaticProps</code> functions to fetch data and pass it as props to your React components before rendering. These functions are executed on the server side before rendering the page.</li>
  <li>Render the page: Next.js handles the server-side rendering for you. Simply run the development server or build your Next.js project, and it will take care of rendering the pages server-side.</li>
</ol>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// pages/index.js</span>
<span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span>

<span class="kd">const</span> <span class="nx">Home</span> <span class="o">=</span> <span class="p">({</span> <span class="nx">data</span> <span class="p">})</span> <span class="o">=&gt;</span> <span class="p">{</span>
  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">h1</span><span class="o">&gt;</span><span class="nx">Welcome</span> <span class="nx">to</span> <span class="nx">Next</span><span class="p">.</span><span class="nx">js</span> <span class="nx">SSR</span><span class="o">!&lt;</span><span class="sr">/h1</span><span class="err">&gt;
</span>      <span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="p">{</span><span class="nx">data</span><span class="p">}</span><span class="o">&lt;</span><span class="sr">/p</span><span class="err">&gt;
</span>    <span class="o">&lt;</span><span class="sr">/div</span><span class="err">&gt;
</span>  <span class="p">);</span>
<span class="p">};</span>

<span class="k">export</span> <span class="k">async</span> <span class="kd">function</span> <span class="nx">getServerSideProps</span><span class="p">()</span> <span class="p">{</span>
  <span class="c1">// Fetch data from an API or any other data source</span>
  <span class="kd">const</span> <span class="nx">response</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">fetch</span><span class="p">(</span><span class="dl">'</span><span class="s1">https://api.example.com/data</span><span class="dl">'</span><span class="p">);</span>
  <span class="kd">const</span> <span class="nx">data</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">response</span><span class="p">.</span><span class="nx">json</span><span class="p">();</span>

  <span class="c1">// Pass the data as props to the page component</span>
  <span class="k">return</span> <span class="p">{</span>
    <span class="na">props</span><span class="p">:</span> <span class="p">{</span>
      <span class="nx">data</span>
    <span class="p">}</span>
  <span class="p">};</span>
<span class="p">}</span>

<span class="k">export</span> <span class="k">default</span> <span class="nx">Home</span><span class="p">;</span>
</code></pre></div></div>

<p>By implementing server-side rendering with JavaScript MVC frameworks like Vue.js and React.js, you can achieve better performance, improved SEO, and enhanced user experience. With the server-side rendering packages provided by these frameworks, you can easily render your applications on the server and provide pre-rendered HTML to the client. So give it a try and enjoy the benefits of server-side rendering!</p>

<p>#JS #SSR #VueJS #ReactJS</p>

  
  <!-- Disqus -->
  
  <div class="post-disqus">
      <section id="disqus_thread"></section>
      <script>

/**
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables */
/*
var disqus_config = function () {
this.page.url = PAGE_URL;  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
*/
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = '//colinchjs.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>

  </div>
  

</div>
</p>
  </div>
`);

const data = {
  title: 'My Blog Post',
  content: 'This is the content of my blog post.'
};

const renderedHtml = template(data);

2. Mustache

Mustache is a logic-less templating engine that focuses on simplicity and ease of use. It aims to provide a clear separation between data and presentation, making it a popular choice for many JavaScript MVC frameworks.

Key Features:

Example code using Mustache:

const template = `
  <div>
    <h1></h1>
    <p><header>
  <div class="header-small">
    <a href="https://colinchjs.github.io">Colin.js</a>
  </div>
</header>
<div class="post">
  <div class="post-title">Implementing server-side rendering with JavaScript MVC frameworks</div>
  <span class="post-date">
    <time>23 Sep 2023</time>
  </span>
  <div class="post-tag">
    <ul>
      
      <li>
        <a href="https://colinchjs.github.io/tags#VueJS">
          <span>VueJS</span>
        </a>
      </li>
      
      
      <li>
        <a href="https://colinchjs.github.io/tags#ReactJS">
          <span>ReactJS</span>
        </a>
      </li>
      
      
    </ul>
  </div>

  <p>In this blog post, we will explore how to implement server-side rendering with popular JavaScript MVC frameworks, focusing on Vue.js and React.js.</p>

<h2 id="server-side-rendering-with-vuejs">Server-side rendering with Vue.js</h2>
<p>Vue.js provides built-in support for server-side rendering through its official package, Vue SSR. To implement server-side rendering with Vue.js, follow these steps:</p>

<ol>
  <li>Set up a server: You need a server to render the Vue application on the server side. You can use Node.js or any other server-side language/framework to create an HTTP server.</li>
  <li>Use Vue SSR package: Install the <code class="language-plaintext highlighter-rouge">vue-server-renderer</code> package using npm or yarn.</li>
  <li>Create an entry file: Set up an entry file for the server-side rendering. This file will import your Vue application and generate the server-rendered HTML. You can use the <code class="language-plaintext highlighter-rouge">createRenderer</code> function provided by <code class="language-plaintext highlighter-rouge">vue-server-renderer</code> to create a renderer instance.</li>
  <li>Render the app: Use the renderer instance to render your Vue application to a string. Pass the app instance and any initial state as parameters to the <code class="language-plaintext highlighter-rouge">renderToString</code> method.</li>
  <li>Inject the rendered HTML: Once the app is rendered to a string, inject it into the HTML template of your server response before sending it to the client.</li>
</ol>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// server.js</span>
<span class="kd">const</span> <span class="nx">Vue</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">vue</span><span class="dl">'</span><span class="p">);</span>
<span class="kd">const</span> <span class="nx">renderer</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">vue-server-renderer</span><span class="dl">'</span><span class="p">).</span><span class="nx">createRenderer</span><span class="p">();</span>

<span class="c1">// Create an app instance</span>
<span class="kd">const</span> <span class="nx">app</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Vue</span><span class="p">({</span>
  <span class="na">template</span><span class="p">:</span> <span class="s2">`&lt;div&gt;Hello, SSR!&lt;/div&gt;`</span>
<span class="p">});</span>

<span class="c1">// Render the app to a string</span>
<span class="nx">renderer</span><span class="p">.</span><span class="nx">renderToString</span><span class="p">(</span><span class="nx">app</span><span class="p">,</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">html</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">console</span><span class="p">.</span><span class="nx">error</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
    <span class="c1">// Handle the error</span>
    <span class="k">return</span><span class="p">;</span>
  <span class="p">}</span>

  <span class="c1">// Inject the rendered HTML into the server response</span>
  <span class="nx">serverResponse</span><span class="p">.</span><span class="nx">send</span><span class="p">(</span><span class="s2">`&lt;html&gt;&lt;body&gt;</span><span class="p">${</span><span class="nx">html</span><span class="p">}</span><span class="s2">&lt;/body&gt;&lt;/html&gt;`</span><span class="p">);</span>
<span class="p">});</span>
</code></pre></div></div>

<h2 id="server-side-rendering-with-reactjs">Server-side rendering with React.js</h2>
<p>React.js also supports server-side rendering through its official package, Next.js. Heres how you can implement server-side rendering with React.js using Next.js:</p>

<ol>
  <li>Set up a Next.js project: Initialize a new Next.js project using the official command line tool or by manually configuring your project.</li>
  <li>Create pages: Next.js handles server-side rendering automatically for the pages inside the <code class="language-plaintext highlighter-rouge">pages</code> directory. Create your React components as separate files inside the <code class="language-plaintext highlighter-rouge">pages</code> directory.</li>
  <li>Fetch data: Use Next.js built-in <code class="language-plaintext highlighter-rouge">getServerSideProps</code> or <code class="language-plaintext highlighter-rouge">getStaticProps</code> functions to fetch data and pass it as props to your React components before rendering. These functions are executed on the server side before rendering the page.</li>
  <li>Render the page: Next.js handles the server-side rendering for you. Simply run the development server or build your Next.js project, and it will take care of rendering the pages server-side.</li>
</ol>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// pages/index.js</span>
<span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span>

<span class="kd">const</span> <span class="nx">Home</span> <span class="o">=</span> <span class="p">({</span> <span class="nx">data</span> <span class="p">})</span> <span class="o">=&gt;</span> <span class="p">{</span>
  <span class="k">return</span> <span class="p">(</span>
    <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
      <span class="o">&lt;</span><span class="nx">h1</span><span class="o">&gt;</span><span class="nx">Welcome</span> <span class="nx">to</span> <span class="nx">Next</span><span class="p">.</span><span class="nx">js</span> <span class="nx">SSR</span><span class="o">!&lt;</span><span class="sr">/h1</span><span class="err">&gt;
</span>      <span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="p">{</span><span class="nx">data</span><span class="p">}</span><span class="o">&lt;</span><span class="sr">/p</span><span class="err">&gt;
</span>    <span class="o">&lt;</span><span class="sr">/div</span><span class="err">&gt;
</span>  <span class="p">);</span>
<span class="p">};</span>

<span class="k">export</span> <span class="k">async</span> <span class="kd">function</span> <span class="nx">getServerSideProps</span><span class="p">()</span> <span class="p">{</span>
  <span class="c1">// Fetch data from an API or any other data source</span>
  <span class="kd">const</span> <span class="nx">response</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">fetch</span><span class="p">(</span><span class="dl">'</span><span class="s1">https://api.example.com/data</span><span class="dl">'</span><span class="p">);</span>
  <span class="kd">const</span> <span class="nx">data</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">response</span><span class="p">.</span><span class="nx">json</span><span class="p">();</span>

  <span class="c1">// Pass the data as props to the page component</span>
  <span class="k">return</span> <span class="p">{</span>
    <span class="na">props</span><span class="p">:</span> <span class="p">{</span>
      <span class="nx">data</span>
    <span class="p">}</span>
  <span class="p">};</span>
<span class="p">}</span>

<span class="k">export</span> <span class="k">default</span> <span class="nx">Home</span><span class="p">;</span>
</code></pre></div></div>

<p>By implementing server-side rendering with JavaScript MVC frameworks like Vue.js and React.js, you can achieve better performance, improved SEO, and enhanced user experience. With the server-side rendering packages provided by these frameworks, you can easily render your applications on the server and provide pre-rendered HTML to the client. So give it a try and enjoy the benefits of server-side rendering!</p>

<p>#JS #SSR #VueJS #ReactJS</p>

  
  <!-- Disqus -->
  
  <div class="post-disqus">
      <section id="disqus_thread"></section>
      <script>

/**
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables */
/*
var disqus_config = function () {
this.page.url = PAGE_URL;  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
*/
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = '//colinchjs.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>

  </div>
  

</div>
</p>
  </div>`;

const data = {
  title: 'My Blog Post',
  content: 'This is the content of my blog post.'
};

const renderedHtml = Mustache.render(template, data);

#javascript #templatingengines