Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using Template::custom without template_dir #1860

Closed
cubetastic33 opened this issue Aug 26, 2021 · 2 comments
Closed

Allow using Template::custom without template_dir #1860

cubetastic33 opened this issue Aug 26, 2021 · 2 comments
Labels
triage A bug report being investigated

Comments

@cubetastic33
Copy link

Description

I want to compile my rocket program into a single binary, so that it is easier to distribute. Since I use tera templates, I tried using Templates::custom so that I can add the templates with include_str! during compile time. Following this example, I was able to get the template working from a string, but it would not run unless template_dir existed. It works even if I create an empty templates directory. I think this shouldn't be a requirement unless external template files are being used, and now I'm unable to make the standalone binary work.

To Reproduce

#[macro_use] extern crate rocket;

use rocket_dyn_templates::{Template, tera::Tera};
use std::collections::HashMap;

#[get("/")]
fn index() -> Template {
    let mut context: HashMap<String, String> = HashMap::new();
    context.insert("foo".to_string(), "bar".to_string());
    Template::render("index.html", context)
}

pub fn customize(tera: &mut Tera) {
    tera.add_raw_template("index.html", "{{ foo }}").expect("valid Tera template");
}

#[launch]
fn rocket() -> _ {
    rocket::build()
        .mount("/", routes![index])
        .attach(Template::custom(|engines| customize(&mut engines.tera)))
}

Expected Behavior

The code would run without a templates directory because I'm not using any external templates

Environment:

  • OS Distribution and Kernel: Artix Linux, 5.13.12-artix1-1
  • Rocket Version: 0.5.0-rc.1

Additional Context

I tried setting template_dir to ., which is a kinda hacky solution but worked. But that seems to be a runtime config setting, so I wasn't able to use it to make the single file binary.

@cubetastic33 cubetastic33 added the triage A bug report being investigated label Aug 26, 2021
@SergioBenitez
Copy link
Member

This looks to be exactly #1792. Is that right?

@cubetastic33
Copy link
Author

Yes, you're right - my bad. Do you know if there's been any progress with the implementation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage A bug report being investigated
Projects
None yet
Development

No branches or pull requests

2 participants