Currently in Beta - View old docs

What is templating?

Templating is the process of creating template files that are reusable and populated at runtime using a Template Engine with values determined by your application logic.

Template files are files that contain placeholder values, such as:

{{ yourVariableName }}

It is these placeholder values that are replaced by the Template Engine.

A Template Engine executes the actual replacement of the variables within the template file. A template engine also transforms the template file into an HTML file which is then sent to the client. Template engines support more than just simply replacing values, they can also handle looping and condtional logic such as for loops and if statements. This makes template engines a good choice for creating HTML files.

A simple example of this is imagine you're creating a website. This website could have 5, 10, 15 or more pages but some things are consistent across these pages. Typically you may want to keep the header and footer the same across all pages but writing out code for the header and footer for each of these pages would be tedious. What happens if you need to change something in the header? You would need to change this on every page of your website. Templating provides a simple solution to this problem. You can create a template file that contains the code for your header and footer but the main content section is determined at runtime using the template engine. An example of this template file using Stencil could look as follows:

<!DOCTYPE html>
        <html lang="en" dir="ltr">
          <head>
            <meta charset="utf-8">
            <title>An Example of Templating</title>
          </head>
          <body>
            <header>
              <h1>Name</h1>
              <nav>
                <li>Home</li>
                <li>About</li>
                <li>Contact</li>
              </nav>
            </header>

            <main>

              {% block content %}{% endblock %}

            </main>

            <footer>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit</p>
              <nav>
                <li>Link1</li>
                <li>Link2</li>
                <li>Link3</li>
              </nav>
            </footer>
          </body>
        </html>

You could reuse this template file to generate every page on your website, but the code for your header and footer would be in a single location. This was a simple example but illustrates how powerful templating can be when creating web applications.

Here is a list of Template Engines Kitura works with:

  • Stencil
  • Mustache
  • Kitura Markdown

Next steps

Add logging: Get useful feedback from your server about startup and errors

Add routing: Add REST APIs, such as HTTP GET, to your server

Back to top