Rocket Frontend: Templates and Static Assets

James Bowen
4 min readAug 24, 2020

In the last few articles, we’ve been exploring the Rocket library for Rust web servers. Last time out, we tried a couple ways to add authentication to our web server. In this last Rocket-specific post, we’ll explore some ideas around frontend templating. This will make it easy for you to serve HTML content to your users!

To explore the code for this article, head over to the “rocket_template” file on our Github repo! If you’re still new to Rust, you might want to start with some simpler material. Take a look at our Rust Beginners Series as well!

Templating Basics

First, let’s understand the basics of HTML templating. When our server serves out a webpage, we return HTML to the user for the browser to render. Consider this simple index page:

<html>
<head></head>
<body>
<p> Welcome to the site!</p>
</body>
</html>

But of course, each user should see some kind of custom content. For example, in our greeting, we might want to give the user’s name. In an HTML template, we’ll create a variable or sorts in our HTML, delineated by braces:

<html>
<head></head>
<body>
<p> Welcome to the site {{name}}!</p>
</body>
</html>

--

--