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

How to embed templates into binary artifacts? #957

Open
skyone-wzw opened this issue Jan 19, 2025 · 0 comments
Open

How to embed templates into binary artifacts? #957

skyone-wzw opened this issue Jan 19, 2025 · 0 comments

Comments

@skyone-wzw
Copy link

I want my program to be a single-file executable program in the end. To do this I need to embed the template into the binary product. Currently, I use include_dir!. But how do I register the template to Tera?

My code is work:

use include_dir::include_dir;
use tera::Tera;

static EMBED_TEMPLATE: include_dir::Dir = include_dir!("templates");

pub fn make_template() -> Tera {
    let mut tera = Tera::default();
    for file in EMBED_TEMPLATE.files() {
        let name = match file.path().to_str() {
            Some(name) => name,
            None => continue,
        };
        let content = match file.contents_utf8() {
            Some(content) => content,
            None => continue,
        };
        if let Err(_) = tera.add_raw_template(name, content) {
            std::process::exit(1);
        }
    }
    tera
}

I can't guarantee the correct dependencies unless I specify the order manually.

When I use {% extend layout.html %} in index.html,the index.html depends on layout.html. But index.html is registered before layout.html in my code. This causes Tera to not be able to find layout.html when parsing index.html.

Is there any way to solve this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant