“Handelbars Helper not defined”

huangapple go评论65阅读模式
英文:

Handelbars Helper not defined

问题

以下是您要翻译的内容:

I am programming a Website using Rust and the Rocket Framework(v0.5). I am using Handlebars 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.

英文:

I am programming a Website using Rust and the Rocket Framework(v0.5). I am using Handlebars 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.

答案1

得分: 1

从:

{{#*inline "page"}}

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

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

更改为:

{{#*inline "page"}}

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

{{/inline}}
{{> layout}}
英文:

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}}

huangapple
  • 本文由 发表于 2023年3月31日 04:46:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定