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

Add 'Environment::render_named_str()' #149

Merged
merged 2 commits into from Nov 19, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions minijinja/src/environment.rs
Expand Up @@ -206,11 +206,30 @@ impl<'source> Environment<'source> {
pub fn render_str<S: Serialize>(&self, source: &str, ctx: S) -> Result<String, Error> {
// 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("<string>", source, Value::from_serializable(&ctx))
}

fn _render_str(&self, source: &str, root: Value) -> Result<String, Error> {
let name = "<string>";
/// Like [`render_str()`](Self::render_str()), but provide a name for the
/// template to be used instead of the default `<string>`.
///
/// ```
/// # use minijinja::{Environment, context};
/// let env = Environment::new();
/// let rv = env.render_str_named("Hello {{ name }}", "template", context! { name => "World" });
/// println!("{}", rv.unwrap());
/// ```
pub fn render_str_named<S: Serialize>(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the template name shouldn't go first. That aligns with add_template.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I teeter-tottered between the two options. The reason I chose this was because the method is named render_str_named, so I wanted to have the str and then the name. But it could easily be argued that after "named" should come the name: render_str_named("foo", "{{ the template }}"). Or maybe the method should be named render_named_str. I don't have any preference.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe render_named_str is a better name anyways. Would probably pick that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

render_named_str(name, template, ctxt) or render_named_str(template, name, ctxt)?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

render_named_str(name, template, ctxt) analog to add_template(name, template)?.render(ctxt)

&self,
source: &str,
name: &str,
ctx: S,
) -> Result<String, Error> {
// 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<String, Error> {
let compiled = ok!(CompiledTemplate::from_name_and_source(name, source));
let mut rv = String::new();
Vm::new(self)
Expand Down