Logo for Sneaky Crow, LLC (vector art of a crow with a green baseball cap on)

Brain Juice

In the first part of this series we did some basic initial research, and then a "kinda-mvp" of a html file generator. We also added a Github Actions config for auto-deployment.

In this part, we're going to create our templating system using handlebars-rust. Afterwards, we'll insert some CSS using SCSS for styling. I'm using grass to compile our SCSS.

I have no particularly reason that I decided on these two libraries beyond I'm familiar with handlebars and that I wanted a simple lib for compiling scss. The reason we're using templating here is mainly for our blog posts, but we'll take advantage of it for pages too.

Our goal for this part of the series is to generate a stylized index.html from a handlebars template.

First, we create our templates directory in the root directory. The reason we're doing it in the root directory is because when rust is parsing relative files it starts there. It basically just makes referring to files clean and easy.

In addition, we know we're going to have SCSS files as well, so let's call the top level folder assets with two subfolders assets/templates and assets/scss.

note, you may notice I re-organized this website repository to match this _ after_ I wrote this article. Shut up, software is iterative. I realized this was better when writing this, lol.

So, our repo should look something like this now:

1
├── assets
2
│ ├── scss
3
│ ├── templates
4
├── cli
5
│ ├── src
6
│ └── Cargo.toml
7
├── Cargo.lock
8
├── Cargo.toml

Next, we'll add an index.hbs file in our templates directory, for our homepage. Since we expect to have multiple pages, (like an about page for example), we want to create the directory assets/templates/pages as well. For the homepage template, we'll basically just take the string we had in stored in the source code from part 1 and move it to the template file. So, we should have the file assets/templates/pages/index.hbs now, and it should look something like this:

1
<html lang="en">
2
  <head>
3
    <title>{{title}}</title>
4
  </head>
5
  <body>
6
    <h1>It works!</h1>
7
  </body>
8
</html>

Next, we need to update our Rust code and introduce handlebars. For handlebars, you create a registry, load templates into that registry, and then use that registry to compile your html. Let's add this logic into our cli/src/main.rs.

1
fn main() {
2
    env_logger::init();
3
    // Create handlebars registry
4
    let mut hbs_registry = Handlebars::new();
5
    // Load page templates directory
6
    hbs_registry
7
        .register_template_directory(".hbs", "assets/templates/pages")
8
        .expect("[HANDLEBARS ERROR] Could not register templates in assets/templates/pages");
9
    let output_dir = "_out";
10
    let html_path = format!("{}/index.html", &output_dir);
11

12
    debug!("[CREATING FILES] {}", html_path);
13
    fs::create_dir_all(output_dir)
14
        .expect("[DIRECTORY CREATE ERROR] Could not create output directory");
15

16
    let mut file =
17
        File::create(html_path).expect("[FILE CREATION ERROR] Could not create html file");
18
    file.write_all(&html.as_bytes())
19
        .expect("[FILE WRITE ERROR] Could not write html to file");
20
}

Great...except it isn't actually using our template yet! Let's fix that. Let's update our template to use a dynamic <title>, so we can validate that the template isn't just spitting itself out. Then we'll render the template with a dynamic title we pass via Serialize struct.

1
fn main() {
2
    env_logger::init();
3
    // Create handlebars registry
4
    let mut hbs_registry = Handlebars::new();
5
    // Load page templates directory
6
    hbs_registry
7
        .register_template_directory(".hbs", "assets/templates/pages")
8
        .expect("[HANDLEBARS ERROR] Could not register templates in assets/templates/pages");
9
    let output_dir = "_out";
10
    let html_path = format!("{}/index.html", &output_dir);
11

12
    debug!("[CREATING FILES] {}", html_path);
13
    // Generate output directory
14
    fs::create_dir_all(output_dir)
15
        .expect("[DIRECTORY CREATE ERROR] Could not create output directory");
16

17
    // Render template with data
18
    let data = HashMap::from([("title", "example title")]);
19
    // Use our registry to render the template with our data
20
    let html = hbs_registry.render("index", &data).expect("Could not generate html");
21

22

23
    let mut file =
24
        File::create(html_path).expect("[FILE CREATION ERROR] Could not create html file");
25
    file.write_all(&html.as_bytes())
26
        .expect("[FILE WRITE ERROR] Could not write html to file");
27
}

After running this, it should generate an html page, using our template, with a <title> of the value example title.

Next, we'll introduce SCSS compilation via the grass package. First, we'll want to create a .scss file in our assets/scss directory. We'll start by adjusting the contrast of the default black/white to a dark gray/beige. You don't have to do this, but you'll want to add something so you know it's working.

I'm creating a file assets/scss/_index.scss where I'll set some variables and make a few basic changes.

1
$black: #1f1f1f;
2
$white: #efefef;
3

4
body {
5
  background-color: $white;
6
  color: $black;
7
}

Next, we'll update our Rust code to read the SCSS in and spit CSS out, which we then save into a .css file.

1
fn main() {
2
    env_logger::init();
3
    // Create handlebars registry
4
    let mut hbs_registry = Handlebars::new();
5
    // Load page templates directory
6
    hbs_registry
7
        .register_template_directory(".hbs", "assets/templates/pages")
8
        .expect("[HANDLEBARS ERROR] Could not register templates in assets/templates/pages");
9
    let output_dir = "_out";
10
    let html_path = format!("{}/index.html", &output_dir);
11

12
    debug!("[CREATING FILES] {}", html_path);
13
    // Generate output directory
14
    fs::create_dir_all(output_dir)
15
        .expect("[DIRECTORY CREATE ERROR] Could not create output directory");
16

17
    // Render template with data
18
    let data = HashMap::from([("title", "example title")]);
19
    // Use our registry to render the template with our data
20
    let html = hbs_registry.render("index", &data).expect("Could not generate html");
21

22

23
    let mut file =
24
        File::create(html_path).expect("[FILE CREATION ERROR] Could not create html file");
25
    file.write_all(&html.as_bytes())
26
        .expect("[FILE WRITE ERROR] Could not write html to file");
27

28
    let css = grass::from_path("assets/scss/_index.scss", &grass::Options::default()).expect(
29
        format!(
30
            "[SASS COMPILATION ERROR] Could not compile sass file {}",
31
            path
32
        )
33
            .as_str(),
34
    );
35
    let css_path = format!("{}/main.css", &output_dir);
36
    let mut css_file =
37
        File::create(css_path).expect("[FILE CREATION ERROR] Could not create html file");
38
    file.write_all(&css.as_bytes())
39
        .expect("[FILE WRITE ERROR] Could not write html to file");
40
}

Lastly, make sure to add the appropriate <link> to our html template

1
<html lang="en">
2
  <head>
3
    <title>{{title}}</title>
4
    <link rel="stylesheet" href="/main.css" />
5
  </head>
6
  <body>
7
    <h1>It works!</h1>
8
  </body>
9
</html>

Congratulations! You should now have a templated static site generator with handlebars and SCSS! In the next tutorial we'll implement a markdown parser so we can write static blog posts in markdown.