-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #655 from cmrschwarz/set_block_parameter
Allow users to set single block params.
- Loading branch information
Showing
4 changed files
with
80 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use handlebars::{ | ||
BlockParamHolder, Context, Handlebars, Helper, Output, RenderContext, RenderError, | ||
RenderErrorReason, | ||
}; | ||
use serde_json::{json, Value}; | ||
|
||
// a custom block helper to bind a variable name to a value | ||
pub fn helper_let<'reg, 'rc>( | ||
h: &Helper<'rc>, | ||
_r: &'reg Handlebars<'reg>, | ||
_ctx: &'rc Context, | ||
rc: &mut RenderContext<'reg, 'rc>, | ||
_out: &mut dyn Output, | ||
) -> Result<(), RenderError> { | ||
let name_param = h | ||
.param(0) | ||
.ok_or_else(|| RenderErrorReason::ParamNotFoundForIndex("let", 0))?; | ||
|
||
let Some(Value::String(name_constant)) = name_param.try_get_constant_value() else { | ||
return Err(RenderErrorReason::ParamTypeMismatchForName( | ||
"let", | ||
"0".to_string(), | ||
"constant string".to_string(), | ||
) | ||
.into()); | ||
}; | ||
|
||
let value = h | ||
.param(1) | ||
.as_ref() | ||
.map(|v| v.value().to_owned()) | ||
.ok_or_else(|| RenderErrorReason::ParamNotFoundForIndex("let", 2))?; | ||
|
||
let block = rc.block_mut().unwrap(); | ||
|
||
block.set_block_param(name_constant, BlockParamHolder::Value(value)); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn main() -> Result<(), RenderError> { | ||
// create the handlebars registry | ||
let mut handlebars = Handlebars::new(); | ||
|
||
handlebars.register_helper("let", Box::new(helper_let)); | ||
|
||
let input = r#" | ||
{{#if foo}} | ||
{{let "mixin_classes" "foo-bar baz"}} | ||
{{else}} | ||
{{let "mixin_classes" "quux"}} | ||
{{/if}} | ||
<div class="content-main {{mixin_classes}}"> | ||
<p> content </p> | ||
</div> | ||
"#; | ||
|
||
println!( | ||
"{}", | ||
handlebars.render_template(input, &json!({"foo": true}))? | ||
); | ||
|
||
Ok(()) | ||
} |
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