Can't seem to use template inheritance from Tera. #1892
-
I have launched a rocket and I get the following error when I access the route:
I have these templates in the default template directory: base.html.tera<!DOCTYPE html>
<html lang="en">
<head>
{% block head %}
<meta charset="utf-8" />
<link rel="stylesheet" href="style/style.css" />
<title>{% block title %}{% endblock title %} - My Webpage</title>
{% endblock head %}
</head>
<body>
<nav>{% block navbar %}{% endblock navbar %}</nav>
<main>{% block main %}{% endblock main %}</main>
<footer>{% block footer %}{% endblock footer %}</footer>
</body>
</html> navbar.html.tera{% extends "base" %}
{% block navbar %}
<nav class="menu">
<a class="focus" href="index.html">Home</a>
</nav>
{% endblock navbar %} main.rs#![cfg_attr(debug_assertions, allow(dead_code, unused_imports))]
#[cfg(test)] mod tests;
#[macro_use] extern crate rocket;
use std::{collections::HashMap, task::Context};
use rocket::{
Request, Data, Response,
fairing::{Fairing, Info, Kind},
http::{Method, ContentType, Status},
fs::{FileServer, relative}
};
use rocket_dyn_templates::{
Template,
tera::{Tera}
};
#[get("/")]
fn index() -> Template {
let mut context = HashMap::new();
context.insert( "base", "navbar");
Template::render("base", &context)
}
#[launch]
fn rocket() -> _ {
let tera = match Tera::new("templates/**/*") {
Ok(t) => t,
Err(e) => {
println!("parsing errors: {}", e);
::std::process::exit(1);
}
};
let names: Vec<_> = tera.get_template_names().collect();
println!("{:?}", names);
rocket::build().mount("/", routes![
index,
])
.mount("/", FileServer::from(relative!("static")))
.attach(Template::fairing())
} I have looked at Tera's documentation, and the Rocket Template section, but I can't find anything that indicates what I have done wrong.
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Also this is my Cargo.toml [package]
name = "hello_rocket"
version = "0.1.1"
edition = "2018"
[dependencies]
rocket = { version = "0.5.0-rc.1", features = ["json"] }
rocket_dyn_templates = { version = "0.1.0-rc.1", features = ["tera"] }
diesel = { version = "1.4.4", features = ["postgres"] }
dotenv = "0.15.0" Thanks if you have an answer :) |
Beta Was this translation helpful? Give feedback.
-
This error is showing because (Aside from that, your templates don't seem quite right. |
Beta Was this translation helpful? Give feedback.
This error is showing because
Tera::new()
androcket_dyn_templates
use different rules for file extensions.Tera::new()
strips one file extension (or none, it seems);rocket_dyn_templates
strips two.(Aside from that, your templates don't seem quite right.
extends
is used for template inheritance, but this navbar looks like a use case forinclude
instead.)