aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--askama/src/lib.rs12
-rw-r--r--askama_shared/src/generator.rs4
-rw-r--r--askama_shared/src/input.rs6
3 files changed, 22 insertions, 0 deletions
diff --git a/askama/src/lib.rs b/askama/src/lib.rs
index f1aa91c..ccfddc8 100644
--- a/askama/src/lib.rs
+++ b/askama/src/lib.rs
@@ -86,6 +86,9 @@ pub trait Template {
/// Provides a conservative estimate of the expanded length of the rendered template
const SIZE_HINT: usize;
+
+ /// The MIME type (Content-Type) of the data that gets rendered by this Template
+ const MIME_TYPE: &'static str;
}
/// Object-safe wrapper trait around [`Template`] implementers
@@ -103,6 +106,9 @@ pub trait DynTemplate {
/// Provides a conservative estimate of the expanded length of the rendered template
fn size_hint(&self) -> usize;
+
+ /// The MIME type (Content-Type) of the data that gets rendered by this Template
+ fn mime_type(&self) -> &'static str;
}
impl<T: Template> DynTemplate for T {
@@ -121,6 +127,10 @@ impl<T: Template> DynTemplate for T {
fn size_hint(&self) -> usize {
Self::SIZE_HINT
}
+
+ fn mime_type(&self) -> &'static str {
+ Self::MIME_TYPE
+ }
}
pub use crate::shared::filters;
@@ -162,6 +172,8 @@ mod tests {
const EXTENSION: Option<&'static str> = Some("txt");
const SIZE_HINT: usize = 4;
+
+ const MIME_TYPE: &'static str = "text/plain; charset=utf-8";
}
fn render(t: &dyn DynTemplate) -> String {
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs
index 5c6303c..cd18819 100644
--- a/askama_shared/src/generator.rs
+++ b/askama_shared/src/generator.rs
@@ -165,6 +165,10 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
buf.writeln(&format!("{}", size_hint))?;
buf.writeln(";")?;
+ buf.writeln("const MIME_TYPE: &'static ::std::primitive::str = ")?;
+ buf.writeln(&format!("{:?}", &self.input.mime_type))?;
+ buf.writeln(";")?;
+
buf.writeln("}")?;
Ok(())
}
diff --git a/askama_shared/src/input.rs b/askama_shared/src/input.rs
index a679461..17410ed 100644
--- a/askama_shared/src/input.rs
+++ b/askama_shared/src/input.rs
@@ -14,6 +14,7 @@ pub struct TemplateInput<'a> {
pub print: Print,
pub escaper: &'a str,
pub ext: Option<String>,
+ pub mime_type: String,
pub parent: Option<&'a syn::Type>,
pub path: PathBuf,
}
@@ -180,6 +181,10 @@ impl TemplateInput<'_> {
CompileError::String(format!("no escaper defined for extension '{}'", escaping,))
})?;
+ let mime_type =
+ extension_to_mime_type(ext_default_to_path(ext.as_deref(), &path).unwrap_or("txt"))
+ .to_string();
+
Ok(TemplateInput {
ast,
config,
@@ -188,6 +193,7 @@ impl TemplateInput<'_> {
print,
escaper,
ext,
+ mime_type,
parent,
path,
})