rusthandlebars.jsrust-rockethtml-templates

How to fix "Helper not defined" error from Handlebars template in Rocket?


I am programming a website using Rust and Rocket (v0.5). I am using Handlebars templates and get this error when opening my site:

>> Matched: (index) GET /
>> Handlebars: Error rendering "index" line 9, col 1: Helper not defined: Path(Relative(([Named("parent")], "parent")))
>> Template 'index' failed to render.
>> Outcome: Failure
>> No 500 catcher registered. Using Rocket default.
>> Response succeeded.

This is the Rust code I am using:

#[macro_use]
extern crate rocket;

use rocket::Request;
use rocket_dyn_templates::Template;

#[derive(serde::Serialize)]
struct BoardContext {
    parent: &'static str,
}

#[derive(serde::Serialize)]
struct AboutContext {
    parent: &'static str,
}

#[derive(serde::Serialize)]
struct LegalsContext {
    parent: &'static str,
}

#[derive(serde::Serialize)]
struct NotFoundContext {
    parent: &'static str,
    url: String,
}

#[get("/")]
fn index() -> Template {
    Template::render("index", &BoardContext { parent: "layout" })
}

#[get("/about")]
fn about() -> Template {
    Template::render("about", &AboutContext { parent: "layout" })
}

#[get("/legals")]
fn legals() -> Template {
    Template::render("legals", &AboutContext { parent: "layout" })
}

#[catch(404)]
fn not_found(req: &Request) -> Template {
    Template::render(
        "not_found",
        NotFoundContext {
            parent: "layout",
            url: req.uri().to_string(),
        },
    )
}

#[launch]
fn rocket() -> _ {
    rocket::build()
        .register("/", catchers![not_found])
        .mount("/", routes![index, about, legals])
        .attach(Template::fairing())
}

This is the index.html.hbs file:

{{#*inline "page"}}

<section id="message_board">
  <h1>Hi, there!</h1>
    Welcome to my meaningless Website!
</section>

{{/inline}}
{{~> (parent)~}}

And this is my layout.html.hbs file:

<!doctype html>
<html>
  <head>
    <title>website</title>
  </head>
  <body>
    {{> header}}
    {{~> page}}
    {{> footer}}
  </body>
</html>

I tried adding a Handlebars helper(like in the example of Rocket) but that didn't resolve the problem.


Solution

  • Answer

    After fiddling a lot around, I found out what the problem was. In the index.html.hbs. From:

    {{#*inline "page"}}
    
    <section id="message_board">
      <h1>Hi, there!</h1>
        Welcome to my meaningless Website!
    </section>
    
    {{/inline}}
    {{~> (parent)~}}
    

    Changed to:

    {{#*inline "page"}}
    
    <section id="message_board">
      <h1>Hi, there!</h1>
        Welcome to my meaningless Website!
    </section>
    
    {{/inline}}
    {{> layout}}