Skip to content

Template Rendering

StratusTS uses EJS (Embedded JavaScript) for rendering dynamic HTML pages. This guide covers everything you need to build server-side rendered applications.

Template rendering generates HTML dynamically on the server:

  • Mix HTML with JavaScript
  • Pass data from controllers to templates
  • Create reusable layouts and components
  • Build traditional web applications

In src/settings.ts:

src/settings.ts
const TEMPLATE_DIR = 'templates';

This tells StratusTS where to find your templates.

terminal
mkdir templates
mkdir templates/layouts
mkdir templates/partials
mkdir templates/pages

Structure:

  • Directorytemplates/
    • Directorylayouts/
      • base.ejs # Main layout
    • Directorypartials/
      • header.ejs # Reusable header
      • footer.ejs # Reusable footer
    • Directorypages/
      • home.ejs # Home page
      • about.ejs # About page

Create templates/pages/home.ejs:

templates/pages/home.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>Welcome to StratusTS!</p>
</body>
</html>
app/controllers.ts
import { type ControllerType } from 'stratus-ts';
const homeController: ControllerType = (question, reply) => {
reply.render('pages/home.ejs');
};
export { homeController };

Layouts provide consistent structure across pages.

Create templates/layouts/base.ejs:

layouts/base.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= title || 'My App' %></title>
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<%- include('../partials/header') %>
<main>
<%- block main %>
</main>
<%- include('../partials/footer') %>
<script src="/js/main.js"></script>
</body>
</html>
pages/home.ejs
<%- layout("layouts/base",{
title: 'home page'
}) %>
<% block main %>
<h2>this is home page content</h2>
<% block main end %>

Reusable template fragments.

Create templates/partials/header.ejs:

pages/home.ejs
<header>
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
<% if (user) { %>
<a href="/profile">Profile</a>
<a href="/logout">Logout</a>
<% } else { %>
<a href="/login">Login</a>
<% } %>
</nav>
</header>
pages/home.ejs
<!-- Include without data -->
<%- include('partials/header') %>
<!-- Include with data -->
<%- include('partials/card', { title: 'Card Title', content: 'Card content' }) %>

Now that you understand template rendering:

  • Plan - Check the status of the project