diff --git a/minijinja/src/environment.rs b/minijinja/src/environment.rs index ea8a148b..4c05f3d9 100644 --- a/minijinja/src/environment.rs +++ b/minijinja/src/environment.rs @@ -206,11 +206,36 @@ impl<'source> Environment<'source> { pub fn render_str(&self, source: &str, ctx: S) -> Result { // reduce total amount of code faling under mono morphization into // this function, and share the rest in _eval. - self._render_str(source, Value::from_serializable(&ctx)) + self._render_str("", source, Value::from_serializable(&ctx)) } - fn _render_str(&self, source: &str, root: Value) -> Result { - let name = ""; + /// Parses and renders a template from a string in one go with name. + /// + /// Like [`render_str`](Self::render_str), but provide a name for the + /// template to be used instead of the default ``. + /// + /// ``` + /// # use minijinja::{Environment, context}; + /// let env = Environment::new(); + /// let rv = env.render_named_str( + /// "template_name", + /// "Hello {{ name }}", + /// context! { name => "World" } + /// ); + /// println!("{}", rv.unwrap()); + /// ``` + pub fn render_named_str( + &self, + name: &str, + source: &str, + ctx: S, + ) -> Result { + // reduce total amount of code faling under mono morphization into + // this function, and share the rest in _eval. + self._render_str(name, source, Value::from_serializable(&ctx)) + } + + fn _render_str(&self, name: &str, source: &str, root: Value) -> Result { let compiled = ok!(CompiledTemplate::from_name_and_source(name, source)); let mut rv = String::new(); Vm::new(self)