“Handelbars Helper not defined”

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

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:

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

This is the rust code I am using:

  1. #[macro_use]
  2. extern crate rocket;
  3. use rocket::Request;
  4. use rocket_dyn_templates::Template;
  5. #[derive(serde::Serialize)]
  6. struct BoardContext {
  7. parent: &'static str,
  8. }
  9. #[derive(serde::Serialize)]
  10. struct AboutContext {
  11. parent: &'static str,
  12. }
  13. #[derive(serde::Serialize)]
  14. struct LegalsContext {
  15. parent: &'static str,
  16. }
  17. #[derive(serde::Serialize)]
  18. struct NotFoundContext {
  19. parent: &'static str,
  20. url: String,
  21. }
  22. #[get("/")]
  23. fn index() -> Template {
  24. Template::render("index", &BoardContext { parent: "layout" })
  25. }
  26. #[get("/about")]
  27. fn about() -> Template {
  28. Template::render("about", &AboutContext { parent: "layout" })
  29. }
  30. #[get("/legals")]
  31. fn legals() -> Template {
  32. Template::render("legals", &AboutContext { parent: "layout" })
  33. }
  34. #[catch(404)]
  35. fn not_found(req: &Request) -> Template {
  36. Template::render(
  37. "not_found",
  38. NotFoundContext {
  39. parent: "layout",
  40. url: req.uri().to_string(),
  41. },
  42. )
  43. }
  44. #[launch]
  45. fn rocket() -> _ {
  46. rocket::build()
  47. .register("/", catchers![not_found])
  48. .mount("/", routes![index, about, legals])
  49. .attach(Template::fairing())
  50. }

This is the index.html.hbs file:

  1. {{#*inline "page"}}
  2. <section id="message_board">
  3. <h1>Hi, there!</h1>
  4. Welcome to my meaningless Website!
  5. </section>
  6. {{/inline}}
  7. {{~> (parent)~}}

And this is my layout.html.hbs file:

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>website</title>
  5. </head>
  6. <body>
  7. {{> header}}
  8. {{~> page}}
  9. {{> footer}}
  10. </body>
  11. </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:

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

This is the rust code I am using:

  1. #[macro_use]
  2. extern crate rocket;
  3. use rocket::Request;
  4. use rocket_dyn_templates::Template;
  5. #[derive(serde::Serialize)]
  6. struct BoardContext {
  7. parent: &'static str,
  8. }
  9. #[derive(serde::Serialize)]
  10. struct AboutContext {
  11. parent: &'static str,
  12. }
  13. #[derive(serde::Serialize)]
  14. struct LegalsContext {
  15. parent: &'static str,
  16. }
  17. #[derive(serde::Serialize)]
  18. struct NotFoundContext {
  19. parent: &'static str,
  20. url: String,
  21. }
  22. #[get("/")]
  23. fn index() -> Template {
  24. Template::render("index", &BoardContext { parent: "layout" })
  25. }
  26. #[get("/about")]
  27. fn about() -> Template {
  28. Template::render("about", &AboutContext { parent: "layout" })
  29. }
  30. #[get("/legals")]
  31. fn legals() -> Template {
  32. Template::render("legals", &AboutContext { parent: "layout" })
  33. }
  34. #[catch(404)]
  35. fn not_found(req: &Request) -> Template {
  36. Template::render(
  37. "not_found",
  38. NotFoundContext {
  39. parent: "layout",
  40. url: req.uri().to_string(),
  41. },
  42. )
  43. }
  44. #[launch]
  45. fn rocket() -> _ {
  46. rocket::build()
  47. .register("/", catchers![not_found])
  48. .mount("/", routes![index, about, legals])
  49. .attach(Template::fairing())
  50. }

This is the index.html.hbs file:

  1. {{#*inline "page"}}
  2. <section id="message_board">
  3. <h1>Hi, there!</h1>
  4. Welcome to my meaningless Website!
  5. </section>
  6. {{/inline}}
  7. {{~> (parent)~}}

And this is my layout.html.hbs file:

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>website</title>
  5. </head>
  6. <body>
  7. {{> header}}
  8. {{~> page}}
  9. {{> footer}}
  10. </body>
  11. </html>

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

答案1

得分: 1

从:

  1. {{#*inline "page"}}
  2. <section id="message_board">
  3. <h1>Hi, there!</h1>
  4. Welcome to my meaningless Website!
  5. </section>
  6. {{/inline}}
  7. {{~> (parent)~}}

更改为:

  1. {{#*inline "page"}}
  2. <section id="message_board">
  3. <h1>Hi, there!</h1>
  4. Welcome to my meaningless Website!
  5. </section>
  6. {{/inline}}
  7. {{> layout}}
英文:

Answer

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

  1. {{#*inline "page"}}
  2. <section id="message_board">
  3. <h1>Hi, there!</h1>
  4. Welcome to my meaningless Website!
  5. </section>
  6. {{/inline}}
  7. {{~> (parent)~}}

Changed to:

  1. {{#*inline "page"}}
  2. <section id="message_board">
  3. <h1>Hi, there!</h1>
  4. Welcome to my meaningless Website!
  5. </section>
  6. {{/inline}}
  7. {{> 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:

确定