From df8b05d0a4243b0d0021871e5cab4269d89b1e0c Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 23 Dec 2016 09:53:46 +0100 Subject: Get template from Cargo manifest-relative source --- askama_codegen/src/lib.rs | 9 +++++++-- askama_test/templates/simple.html | 1 + askama_test/tests/simple.html | 1 - 3 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 askama_test/templates/simple.html delete mode 100644 askama_test/tests/simple.html diff --git a/askama_codegen/src/lib.rs b/askama_codegen/src/lib.rs index 4a17b18..b325bae 100644 --- a/askama_codegen/src/lib.rs +++ b/askama_codegen/src/lib.rs @@ -10,6 +10,7 @@ extern crate syn; use proc_macro::TokenStream; use std::fs::File; use std::io::Read; +use std::path::{Path, PathBuf}; fn get_path_from_attrs(attrs: &Vec) -> String { for attr in attrs { @@ -39,7 +40,11 @@ fn get_path_from_attrs(attrs: &Vec) -> String { panic!("template annotation not found"); } -fn get_template_source(path: &str) -> String { +fn get_template_source(tpl_file: &str) -> String { + let root = ::std::env::var("CARGO_MANIFEST_DIR").unwrap(); + let mut path = PathBuf::from(root); + path.push("templates"); + path.push(Path::new(tpl_file)); let mut f = File::open(path).unwrap(); let mut s = String::new(); f.read_to_string(&mut s).unwrap(); @@ -64,7 +69,7 @@ pub fn derive_template(input: TokenStream) -> TokenStream { let expanded = quote! { impl #impl_generics askama::Template for #name #ty_generics #where_clause { fn render(&self) -> String { - src + #src.into() } } }; diff --git a/askama_test/templates/simple.html b/askama_test/templates/simple.html new file mode 100644 index 0000000..e544306 --- /dev/null +++ b/askama_test/templates/simple.html @@ -0,0 +1 @@ +hello world, {{ var }} diff --git a/askama_test/tests/simple.html b/askama_test/tests/simple.html deleted file mode 100644 index e544306..0000000 --- a/askama_test/tests/simple.html +++ /dev/null @@ -1 +0,0 @@ -hello world, {{ var }} -- cgit