Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a render! macro and a render_str method to env (#89)
- Loading branch information
Showing
8 changed files
with
249 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "render-macro" | ||
version = "0.1.0" | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
minijinja = { path = "../../minijinja" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# render-macro | ||
|
||
A simple example of how to render a single template from a string and render it | ||
with the `render!` macro. | ||
|
||
```console | ||
$ cargo run | ||
Hello John! | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
use minijinja::render; | ||
|
||
fn main() { | ||
println!("{}", render!("Hello {{ name }}!", name => "John")); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
#[cfg(test)] | ||
use similar_asserts::assert_eq; | ||
|
||
/// Hidden utility module for the [`context!`](crate::context!) macro. | ||
#[doc(hidden)] | ||
pub mod __context { | ||
use crate::key::Key; | ||
use crate::value::{RcType, Value, ValueMap, ValueRepr}; | ||
use crate::Environment; | ||
|
||
#[inline(always)] | ||
pub fn make() -> ValueMap { | ||
ValueMap::default() | ||
} | ||
|
||
#[inline(always)] | ||
pub fn add(ctx: &mut ValueMap, key: &'static str, value: Value) { | ||
ctx.insert(Key::Str(key), value); | ||
} | ||
|
||
#[inline(always)] | ||
pub fn build(ctx: ValueMap) -> Value { | ||
ValueRepr::Map(RcType::new(ctx)).into() | ||
} | ||
|
||
pub fn thread_local_env() -> Environment<'static> { | ||
thread_local! { | ||
static ENV: Environment<'static> = Environment::new() | ||
} | ||
ENV.with(|x| x.clone()) | ||
} | ||
} | ||
|
||
/// Creates a template context with keys and values. | ||
/// | ||
/// ```rust | ||
/// # use minijinja::context; | ||
/// let ctx = context! { | ||
/// name => "Peter", | ||
/// location => "World", | ||
/// }; | ||
/// ``` | ||
/// | ||
/// Alternatively if the variable name matches the key name it can | ||
/// be omitted: | ||
/// | ||
/// ```rust | ||
/// # use minijinja::context; | ||
/// let name = "Peter"; | ||
/// let ctx = context! { name }; | ||
/// ``` | ||
/// | ||
/// The return value is a [`Value`](crate::value::Value). | ||
/// | ||
/// Note that [`context!`](crate::context!) can also be used recursively if you need to | ||
/// create nested objects: | ||
/// | ||
/// ```rust | ||
/// # use minijinja::context; | ||
/// let ctx = context! { | ||
/// nav => vec![ | ||
/// context!(path => "/", title => "Index"), | ||
/// context!(path => "/downloads", title => "Downloads"), | ||
/// context!(path => "/faq", title => "FAQ"), | ||
/// ] | ||
/// }; | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! context { | ||
() => { | ||
$crate::__context::build($crate::__context::make()) | ||
}; | ||
( | ||
$($key:ident $(=> $value:expr)?),* $(,)? | ||
) => {{ | ||
let mut ctx = $crate::__context::make(); | ||
$( | ||
$crate::__context_pair!(ctx, $key $(, $value)?); | ||
)* | ||
$crate::__context::build(ctx) | ||
}} | ||
} | ||
|
||
#[macro_export] | ||
#[doc(hidden)] | ||
macro_rules! __context_pair { | ||
($ctx:ident, $key:ident) => {{ | ||
$crate::__context_pair!($ctx, $key, $key); | ||
}}; | ||
($ctx:ident, $key:ident, $value:expr) => { | ||
$crate::__context::add( | ||
&mut $ctx, | ||
stringify!($key), | ||
$crate::value::Value::from_serializable(&$value), | ||
); | ||
}; | ||
} | ||
|
||
/// A macro similar to [`format!`] but that uses MiniJinja for rendering. | ||
/// | ||
/// This can be used to quickly render a MiniJinja template into a string | ||
/// without having to create an environment first which can be useful in | ||
/// some situations. Note however that the template is re-parsed every | ||
/// time the [`render!`](crate::render) macro is called which is potentially | ||
/// slow. | ||
/// | ||
/// There are two forms for this macro. The default form takes template | ||
/// source and context variables, the extended form also lets you provide | ||
/// a custom environment that should be used rather than a default one. | ||
/// The context variables are passed the same way as with the | ||
/// [`context!](crate::context) macro. | ||
/// | ||
/// # Example | ||
/// | ||
/// Passing context explicitly: | ||
/// | ||
/// ``` | ||
/// # use minijinja::render; | ||
/// println!("{}", render!("Hello {{ name }}!", name => "World")); | ||
/// ``` | ||
/// | ||
/// Passing variables with the default name: | ||
/// | ||
/// ``` | ||
/// # use minijinja::render; | ||
/// let name = "World"; | ||
/// println!("{}", render!("Hello {{ name }}!", name)); | ||
/// ``` | ||
/// | ||
/// Passing an explicit environment: | ||
/// | ||
/// ``` | ||
/// # use minijinja::{Environment, render}; | ||
/// let env = Environment::new(); | ||
/// println!("{}", render!(in env, "Hello {{ name }}!", name => "World")); | ||
/// ``` | ||
/// | ||
/// # Panics | ||
/// | ||
/// This macro panics if the format string is an invalid template or the | ||
/// template evaluation failed. | ||
#[macro_export] | ||
macro_rules! render { | ||
( | ||
in $env:expr, | ||
$tmpl:expr | ||
$(, $key:ident $(=> $value:expr)?)* $(,)? | ||
) => { | ||
($env).render_str($tmpl, $crate::context! { $($key $(=> $value)? ,)* }) | ||
.expect("failed to render expression") | ||
}; | ||
( | ||
$tmpl:expr | ||
$(, $key:ident $(=> $value:expr)?)* $(,)? | ||
) => { | ||
$crate::render!(in $crate::__context::thread_local_env(), $tmpl, $($key $(=> $value)? ,)*) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_context() { | ||
use crate::value::Value; | ||
let var1 = 23; | ||
let ctx = context!(var1, var2 => 42); | ||
assert_eq!(ctx.get_attr("var1").unwrap(), Value::from(23)); | ||
assert_eq!(ctx.get_attr("var2").unwrap(), Value::from(42)); | ||
} | ||
|
||
#[test] | ||
fn test_render() { | ||
let env = crate::Environment::new(); | ||
let rv = render!(in env, "Hello {{ name }}!", name => "World"); | ||
assert_eq!(rv, "Hello World!"); | ||
|
||
let rv = render!("Hello {{ name }}!", name => "World"); | ||
assert_eq!(rv, "Hello World!"); | ||
|
||
let rv = render!("Hello World!"); | ||
assert_eq!(rv, "Hello World!"); | ||
} |